LeetCode466. Count The Repetitions
- 题目链接
- 题意
- 定义一个特殊的串, 现在给出串S1和S2的参数, 问: S2最多可以多少个连接起来扔是S1的子序列, 求出这个最大值
- 解题思路
- 注意s1与S1的区别, 可以去看题目描述, 预处理出s1从s2的每一个字符开始匹配, s1可以匹配到s2串尾的数量, 即如果从s2的当前下标开始匹配, 这一个s1最多可以匹配多少s2, 这个s1匹配完成后匹配到了s2的哪一个字符, 下一个s1匹配s2的话应该从哪一个下标开始, 因为串长最长100, 这个预处理时间消耗是很少的, 有了这个预处理的结果, 我们就可以O(n)的知道S1可以匹配多少个s2, 进而推算出匹配多少个S2.
- AC代码
class Solution {
public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
int answer = 0; ArrayList<Integer> numArrayList = new ArrayList<Integer>();
ArrayList<Integer> idxArrayList = new ArrayList<Integer>(); for(int i = 0; i < s2.length(); ++i) {
int num = 0, idx = i;
for(int j = 0; j < s1.length(); ++j) {
if(s2.charAt(idx) == s1.charAt(j)) {
idx++;
if(idx == s2.length()) {
num++;
idx = 0;
}
}
}
numArrayList.add(num);
idxArrayList.add(idx);
} int idx = 0; for(int i = 0; i < n1; ++i) {
answer += numArrayList.get(idx);
idx = idxArrayList.get(idx);
} return answer / n2;
}
}
LeetCode466. Count The Repetitions的更多相关文章
- CH5702 Count The Repetitions
题意 5702 Count The Repetitions 0x50「动态规划」例题 描述 定义 conn(s,n) 为 n 个字符串 s 首尾相接形成的字符串,例如: conn("abc& ...
- [Swift]LeetCode466. 统计重复个数 | Count The Repetitions
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- 466. Count The Repetitions
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- [LeetCode] Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- Leetcode: Count The Repetitions
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- CH5702 Count The Repetitions[倍增dp]
http://contest-hunter.org:83/contest/0x50%E3%80%8C%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E3%80%8D%E4%B ...
- 【leetcode 字符串】466. Count The Repetitions
https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...
- 第七周 Leetcode 466. Count The Repetitions 倍增DP (HARD)
Leetcode 466 直接给出DP方程 dp[i][k]=dp[i][k-1]+dp[(i+dp[i][k-1])%len1][k-1]; dp[i][k]表示从字符串s1的第i位开始匹配2^k个 ...
- [LeetCode] 466. Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
随机推荐
- 使用node.js中遇到的一些小bug
1.BUG Cannot set headers after they are sent to the client 解决:即发出一次请求得到两次或以上的回应时会出现此警告,此时注意查看再在一些条件下 ...
- Spring-Cloud-Netflix-系统架构
目录 系统架构 概述 集中式架构 概述 特点 垂直拆分 概述 特点 系统架构分类 微服务 微服务的特点: 分布式服务: 微服务和分布式的区别: 微服务要面临的问题: springClould是什么 远 ...
- 系统警告,Bronya请求支援(两遍最短路)
系统警告,Bronya请求支援 Description 休伯利安号的一行人来到了由逆熵镇守的前文明遗迹[海渊城],他们准备用巨大的传送装置[海渊之眼]进入量子之海,寻找丢失的渴望宝石.然而在行动前夜爱 ...
- 在非主线程里面使用NSTimer创建和取消定时任务
为什么要在非主线程创建NSTimer 将 timer 添加到主线程的Runloop里面本身会增加线程负荷 如果主线程因为某些原因阻塞卡顿了,timer 定时任务触发的时间精度肯定也会受到影响 有些定时 ...
- Java8 学习笔记--函数式接口
通过之前的函数式接口与lambda表达式的关系那篇文章,大家应该对函数式接口有了一定的了解了,在Java中它是lambda表达式的基础,没有函数式接口就没有办法使用lambda表达式. 函数式接口如此 ...
- Java反射中getDeclaredField和getField的区别
getDeclaredField是可以获取一个类的所有字段. getField只能获取类的public 字段. public Field getDeclaredField(String name) t ...
- Ubuntu16.04安装Vmware Tools
开启虚拟机 安装VMware Tools 在虚拟机名称上,右键>>安装VMware Tools 此时,Ubuntu会提示已经插入光盘,并弹出文件管理页面. 此时我们打开终端查看分区挂载情况 ...
- NS网络仿真,小白起步版,模拟仿真之间注意的事项
FTP是基于TCP的,所以FTP应用不可以绑定UDP发送代理 FTP和CBR属于应用流,他们用来绑定TCP和UDP发送代理 TCP用于发送代理时,接收代理为TCPSink,可以绑定FTP应用.CBR流 ...
- webstorm 永久激活方法
打开终端,执行: cd /etc/ sudo vim hosts 在最后一行加上: 0.0.0.0 account.jetbrains.com 打开webstorm,选择Activation Code ...
- 安卓开发学习日记 DAY4——Button,ImageButton
Button与ImageButton基本类似 也有类似于TextView和ImageView的区别 这里需要注意的是: 在你定义text属性的内容时,最好是在Values文件下的String.xml中 ...