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. windowsServer ------ 安装IIS

    1.找到服务器管理器,点击添加角色,一步步执行 2.添加IIS 相关组件 勾选web服务器 下一步 将web服务iis 相关组件全部勾选,ftp 可不选 选择好后安装 等一会 关闭 可以查看到所安装角 ...

  2. Linux RAID 磁盘管理

    Linux RAID 磁盘管理 RAID工作模式介绍:https://www.cnblogs.com/xiangsikai/p/8441440.html 本章主要讲解 Linux下 RAID5 与 R ...

  3. 基于Spark的电影推荐系统(推荐系统~4)

    第四部分-推荐系统-模型训练 本模块基于第3节 数据加工得到的训练集和测试集数据 做模型训练,最后得到一系列的模型,进而做 预测. 训练多个模型,取其中最好,即取RMSE(均方根误差)值最小的模型 说 ...

  4. VS2013(InstallShield2015LimitedEdition)打包程序详解

    VS2012没有自带打包工具,所以要先下载并安装一个打包工具.我采用微软提供的打包工具:  InstallShield2015LimitedEdition.下载地址:https://msdn.micr ...

  5. 使用 docker-compose 运行 MySQL

    使用 docker-compose 运行 MySQL 目录结构 . │ .env │ docker-compose.yml │ └─mysql ├─config │ my.cnf │ └─data m ...

  6. Qt for Android使用grpc探索

    利用Qt在Android上使用grpc需要*.a的静态库,Windows上编译的lib库以及linux编译出来的.a,经过尝试,均无法链接成功.本文尝试使用NDK来编译Android版本的grpc静态 ...

  7. 奖金高达150万元的NEST即将上线,速来报名

    表哥.表姐们快粗来~~~分享一个劲爆消息: 年度奖金总额高达150万元的众测平台(NEST)已于2019年6月10日启动全球报名,6月26号开始首批线上众测,欢迎精英白帽们前来挑战! 更多详情请认真阅 ...

  8. i春秋-“百度杯”CTF比赛 十月场-Login

    源码发下提示 尝试登陆 得到个什么鬼, 但是相应包里发现个可疑的东西   //  CTF中的0 和1 这些一般都有套路的 然后在请求头里 改为 1 ##代码审计来了..   分析了半天 后来看了别人的 ...

  9. vue学习指南:第一篇 - vue的介绍

    三大主流框架: 1. Vue.js 是目前最火的一个前端框架,react是最流行的前端框架 (react除了开发网站,还可以开发手机app,Vue语法也是可以用于手机App开发的,需要借助于wexx) ...

  10. bootstrap基础样式学习(一)

    1.按钮btn .btn 按钮基础的样式 .btn-default 白底黑子的按钮 .btn-danger/warning/success/info/primary .btn-lg/sm/xs 按钮大 ...