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. Java-100天知识进阶-GC种类-知识铺(六)

    知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停的来唤醒你记忆深处的知识点. 一.GC回收器的 4个指标: 1.Throughput,非gc时间与总运行时间的比重. ...

  2. FilterRegistrationBean注册过滤器探究

    官方定义: A ServletContextInitializer to register Filters in a Servlet 3.0+ container. Similar to the re ...

  3. ASP.NET MVC 中的过滤器

    这里用实例说明各种过滤器的用法,有不对的地方还请大神指出,共同探讨. 1. ActionFilter 方法过滤器: 接口名为 IActionFilter ,在控制器方法调用前/后执行. 在新建的MVC ...

  4. 面试官常问的Nginx的几个问题

    1.什么是Nginx? Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器 Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3 ...

  5. 3-Consul 使用手册

    原文:http://www.liangxiansen.cn/2017/04/06/consul/ Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键 ...

  6. Lambda(一)lambda表达式初体验

    Lambda(一)lambda表达式初体验 Lambda引入 : 随着需求的不断改变,代码也需要随之变化 需求一:有一个农场主要从一堆苹果中挑选出绿色的苹果 解决方案:常规做法,source code ...

  7. Laravel 创建指定表 migrate

    解决方案:打开创建表的那个 migration 文件,在创建表的方法执行之前加一个判断条件 if (!Schema::hasTable('password_resets')) { Schema::cr ...

  8. Mobx总结以及mobx和redux区别

    Mobx解决的问题 传统react使用的数据管理库为Redux.Redux要解决的问题是统一数据流,数据流完全可控并可追踪.要实现该目标,便需要进行相关的约束 Redux由此引出dispatch ac ...

  9. 【转】Git使用教程之BUG分支

    1.bug分支 在开发中,会经常碰到bug问题,那么有了bug就需要修复,在Git中,分支是很强大的,每个bug都可以通过一个临时分支来修复,修复完成后,合并分支,然后将临时的分支删除掉. 比如我在开 ...

  10. iOS tableView侧滑删除的第三方控件

    (到我的文件中,下载“tableview中cell测滑删除的第三方控件”),使用方法如下: 在tableView中的.m中,设置cell的方法上,事例代码如下,其中,EaseConversationC ...