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] Count The Repetitions 计数重复个数的更多相关文章

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

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

  2. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

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

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  4. [Leetcode] count and say 计数和说

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  5. Leetcode: Count The Repetitions

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

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

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

  7. Leetcode 466.统计重复个数

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

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

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

  9. LeetCode Count of Range Sum

    原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...

随机推荐

  1. Android 中关于static的使用问题

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5251564.html 项目中,在不停地接收串口数据很长一段时间(几小时)后,会偶然性的报错.初步排除了oom ...

  2. 20个非常有用的Java程序片段

    下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric strin ...

  3. centos下升级mysql后遇到的小问题

    记录今天遇到的一个小问题, 写一个app访问接口涉及到通过存储过程反馈多个结果集,但是反回多个结果集的存储过程,调用之后只能反回一个了,而且奇怪的是,即使直接在mysql上同时执行两条查询语句,第一条 ...

  4. python之最强王者(7)——元组(tuple)

    1.序列(sequence): 说明:在前面的字符串列表中其实我们已经用到了序列,之所以放到这篇来讲主要是为了承上启下,方便理解和记忆. python的数据访问模型:直接存取 ,序列 ,映射 对非容器 ...

  5. Python3.5安装及opencv安装

    Python安装注意事项(版本3.5,系统windows)1.安装好Python后将D:\Program Files\Python.D:\Program Files\Python\Scripts加入P ...

  6. 图表插件Charts.js的使用

    Charts.js的介绍自行百度 首先下载Charts.js,官网:http://chartjs.cn/ charts.js 托管在了github上,下载下来后加解压出src中的文件即可.其中有cha ...

  7. 9.2.1 .net framework下的MVC 控件的封装(上)

    在写.net core下mvc控件的编写之前,我先说一下.net framework下我们MVC控件的做法. MVC下控件的写法,主要有如下三种,最后一种是泛型的写法,mvc提供的控件都是基本控件. ...

  8. 关于XHR对象中status范围的记录

    if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){ // 成功执行区域 // 2XX表示有效响应 ...

  9. CSS3鼠标滑过动画线条边框特效

    基于CSS属性animation动画制作,效果流畅弹性十足 效果展示 http://hovertree.com/texiao/css3/32/ 源码下载:http://hovertree.com/h/ ...

  10. Js: Extensible Calendar Examples

    http://ext.ensible.comhttps://github.com/bmoeskau/Extensiblehttps://github.com/TeamupCom/extensibleh ...