Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc", 3] ="abcabcabc".

On the other hand, we define that string s1 can be obtained from string s2 if we can remove some characters from s2 such that it becomes s1. For example, “abc” can be obtained from “abdbec” based on our definition, but it can not be obtained from “acbbe”.

You are given two non-empty strings s1 and s2 (each at most 100 characters long) and two integers 0 ≤ n1 ≤ 106and 1 ≤ n2 ≤ 106. Now consider the strings S1 and S2, where S1=[s1,n1] and S2=[s2,n2]. Find the maximum integer M such that [S2,M] can be obtained from S1.

Example:

Input:
s1="acb", n1=4
s2="ab", n2=2 Return:
2

这道题放了好久才写,主要是因为这道题难度确实不小,光是分析研究网上大神们的帖子就搞了好久,就是现在也不能说完全理解了这道题,哎,将就着写吧,有不足的地方欢迎指正。主要参考了网上 大神 lzl124631x 的帖子,还有 大神 aaaeeeo 的帖子。  这道题的 Java 版本的 brute force 可以通过 OJ,但是 C++ 的就不行了,这里需要使用重复模式来优化,通过分析可知:

如果 s2 在 S1 中出现了N次,那么 S2 肯定在 S1 中出现了 N/n2 次,注意这里的大写表示字符串加上重复次数组成的大字符串。

所以可以得出结论,只要算出 s2 出现的次数,然后除以 n2,就可以得出 S2 出现的次数了。

那么问题就是表示重复,遍历 s1 字符串 n1 次,表示每个 s1 字符串为一段,对于每段,可以得知:

1. 出现在该段的 s2 字符串的累计出现次数

2. 一个 nextIndex,其中 s2[nextIndex] 表示在下一段 s1 中你所要寻找的 s2 中的一个字符。(比如说 s1="abc", s2="bac", 由于第一个 s1 中只能匹配上 s2 中的b,那么只有在下一段 s1 中才能继续匹配 s2 中的a,所以 nextIndex=1,即a在 s2 中的位置为1;同理,比如  s1="abca", s2="bac",第一个 s1 可以匹配上 s2 中的 ba,那么后面的c只能在下一段 s1 中匹配上,那么 nextIndex=2,即c在 s2 中的位置为2)

表示重复关键就在于 nextIndex,比如对于下面这个例子:

Input:
s1="abacb", n1=
s2="bcaa", n2= Return:
j --------------->
S1 --------------> abacb | abacb | abacb | abacb | abacb | abacb repeatCount -----> | | | | | nextIndex -------> | | | | |

nextIndex 的范围从0到 s2.size()-1,根据鸽巢原理(又称抽屉原理),你一定会找到相同的两个 nextIndex 在遍历 s1 段 s2.size()+1 次之后。在上面的例子中,重复的 nextIndex 出现在第三段,和第一段一样都为2,那么重复的 pattern 就找到了,是第二段和第三段中的 aabc,而且从第四段开始,每两段就有一个 aabc,现在的目标就是统计出整个 S1 中有多少个s2。

由于 pattern 占用了两段,所以 interval 为2,然后看整个 S1 中有多少个这样的两段,repeat = (n1 - start) / interval。start 表示 pattern 的起始段数,之前的不是 pattern,然后算在整个 S1 中有多少个 pattern 出现,patternCnt = (repeatCnt[k] - repeatCnt[start]) * repeat,注意这里的 repeatCnt[k] - repeatCnt[start] 表示一个 pattern 中有多少个字符串 s2,个人感觉一般来说都是1个。然后算出剩下的非 pattern 的字符串里能包含几个 s2,remainCnt = repeatCnt[start + (n1 - start) % interval],然后把 patternCnt + remainCnt 之和算出来除以 n2 就是需要的结果啦。如果 pattern 未曾出现,那么我们直接用 repeatCnt[n1] / n2 也能得到正确的结果,参见代码如下:

class Solution {
public:
int getMaxRepetitions(string s1, int n1, string s2, int n2) {
vector<int> repeatCnt(n1 + , );
vector<int> nextIdx(n1 + , );
int j = , cnt = ;
for (int k = ; k <= n1; ++k) {
for (int i = ; i < s1.size(); ++i) {
if (s1[i] == s2[j]) {
++j;
if (j == s2.size()) {
j = ;
++cnt;
}
}
}
repeatCnt[k] = cnt;
nextIdx[k] = j;
for (int start = ; start < k; ++start) {
if (nextIdx[start] == j) {
int interval = k - start;
int repeat = (n1 - start) / interval;
int patternCnt = (repeatCnt[k] - repeatCnt[start]) * repeat;
int remainCnt = repeatCnt[start + (n1 - start) % interval];
return (patternCnt + remainCnt) / n2;
}
}
}
return repeatCnt[n1] / n2;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/466

参考资料:

https://leetcode.com/problems/count-the-repetitions/

https://leetcode.com/problems/count-the-repetitions/discuss/95397/C%2B%2B-0ms-O(str1.length*str2.length)

https://leetcode.com/problems/count-the-repetitions/discuss/95401/Ugly-Java-brute-force-solution-but-accepted.-1088ms.

https://leetcode.com/problems/count-the-repetitions/discuss/95402/very-clean-and-short-7ms-java-solution-based-on-70664914-s-idea

https://leetcode.com/problems/count-the-repetitions/discuss/95398/C%2B%2B-solution-inspired-by-%4070664914-with-organized-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 466. Count The Repetitions 计数重复个数的更多相关文章

  1. [LeetCode] Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  2. 第七周 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个 ...

  3. [LeetCode]Count and Say 计数和发言

    Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...

  4. 【leetcode 字符串】466. Count The Repetitions

    https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...

  5. [LeetCode] 38. Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  6. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  7. 466. Count The Repetitions

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  8. Java实现 LeetCode 466 统计重复个数

    466. 统计重复个数 定义由 n 个连接的字符串 s 组成字符串 S,即 S = [s,n].例如,["abc", 3]="abcabcabc". 另一方面, ...

  9. Leetcode 466.统计重复个数

    统计重复个数 定义由 n 个连接的字符串 s 组成字符串 S,即 S = [s,n].例如,["abc", 3]="abcabcabc". 另一方面,如果我们可 ...

随机推荐

  1. PM学习笔记(一):解构产品经理

    1.产品定义:什么是产品 来自百度百科(链接)的解释:        产品是指能够供给市场 [1]  ,被人们使用和消费,并能满足人们某种需求的任何东西,包括有形的物品.无形的服务.组织.观念或它们的 ...

  2. 解决Chrome插件安装时程序包无效【CRX_HEADER_INVALID】的错误

    将[.crx]后缀的文件拖拽至谷歌浏览器开发者模式下的扩展程序管理页签时,报错[CRX_HEADER_INVALID],即此插件无效的错误. 安装失败的原因 原因在于谷歌浏览器在新版本中添加对第三方插 ...

  3. Oracle查询日期字段是否包含时分秒 TRUNC() 函数

    可以使用 ORACLE TRUNC()函数 来进行判断 表 A 日期字段 datetime 部分数据带时分秒,部分数据没有时分秒 select * from A where datetime = TR ...

  4. Oracle - 数字处理 - 取上取整、向下取整、保留N位小数、四舍五入、数字格式化

    用oracle sql对数字进行操作: 取上取整.向下取整.保留N位小数.四舍五入.数字格式化 取整(向下取整): select floor(5.534) from dual; select trun ...

  5. windows下安装pip教程

    下载地址是:https://pypi.python.org/pypi/pip#downloads 下载完成之后,解压到一个文件夹,用CMD控制台进入解压目录,输入: python setup.py i ...

  6. Linux目录和文件——查询目录和文件的命令

    Linux目录和文件——查询目录和文件的命令 摘要:本文主要学习了在Linux系统中是如何查询目录和文件的. which命令 which命令是根据PATH环境变量设置的路径,去搜索执行文件. 基本语法 ...

  7. 微信小程序使用函数防抖解决重复点击消耗性能问题

    wxml: <view bindtap="doubleTap" bindtouchstart="touchStart" bindtouchend=&quo ...

  8. redis笔记3

    redis持久化机制 redis提供了两种持久化策略 RDB RDB的持久化策略: 按照规则定时将内存的数据同步到磁盘 snapshot redis在指定的情况下会触发快照 自己配置的快照规则 sav ...

  9. 关于项目中js原型的使用

    在一个项目中为了减少全局变量的使用及模块化的开发我们使用的构造函数加原型的开发模式 var App = function(){ //管理构造函数的属性 this.name = 'jack' } //页 ...

  10. 想入门Web安全,这些基础知识都学会了吗?

    毕业季已经正式告一段落,这届毕业生都找到心仪的工作了吗? 正在实习期或者试用期的职场新人,是否在岗位上做的风生水起? 工作了一两年,从未升职加薪的菜鸟,还愿意继续原地踏步吗? 在校学生.IT从业者.毕 ...