466. Count The Repetitions
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 ≤ 106 and 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
Approach #1: string. [C++]
class Solution {
public:
int getMaxRepetitions(string s1, int n1, string s2, int n2) {
vector<int> repeatCount(n1+1, 0);
vector<int> nextIndex(n1+1, 0);
int j = 0, cnt = 0;
for (int k = 1; k <= n1; ++k) {
for (int i = 0; i < s1.size(); ++i) {
if (s1[i] == s2[j]) ++j;
if (j == s2.size()) {
j = 0;
++cnt;
}
}
repeatCount[k] = cnt;
nextIndex[k] = j;
for (int start = 0; start < k; ++start) {
if (nextIndex[start] == j) {
int prefixCount = repeatCount[start];
int patternCount = (n1 - start) / (k - start) * (repeatCount[k] - prefixCount);
int suffixCount = repeatCount[start + (n1 - start) % (k - start)] - prefixCount;
return (prefixCount + patternCount + suffixCount) / n2;
}
}
} return repeatCount[n1] / n2;
}
};
Analysis:
Fact:
If s2 repeats in S1 R times, then S2 must repeats in S1 R / n2 times.
Conclusion:
We can simply count the repeation of s2 and then divide the count by n2.
How to denote repeatition:
We need to scan s1 n1 times. Denote each scanning of s1 as an s1 segment.
After each scanning of i-th s1 segment, we will have the accumulative count of s2 repeated in this s1 segment.
A nextIndex that s2[nextIndex] is the first letter you'll be looking for in the next s1 segment.
Suppose s1="abc", s2="bac", nextIndex will be 1; s1="abca", s2="bac", nextIndex will be 2.
It is the nextIndex that is the denotation of the repetitive pattern.
Example:
Input:
s1 = "abacb", n1 = 6
s2 = "bcaa", n2 = 1
Return:
3
0 1 2 3 0 1 2 3 0 1 2 3 0
S1 --------------> abacb | abacb | abacb | abacb | abacb | abacb
repeatCount -----> 0 | 1 | 1 | 2 | 2 | 3
nextIndex -------> 2 | 1 | 2 | 1 | 2 | 1
Once you meet a nextIndex you've met before, you'll know that the following nextIndex ans increments of repeatCount will repeat a pattern.
So let's seperate the s1 segments into 3 parts:
the prefix part before repetitive pattern
the repetitive part
the suffix part after repetitive pattertn (incomplete pattern remnant).
Reference:
https://leetcode.com/problems/count-the-repetitions/discuss/95398/C%2B%2B-solution-inspired-by-%4070664914-with-organized-explanation
466. Count The Repetitions的更多相关文章
- 第七周 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 ...
- 【leetcode 字符串】466. Count The Repetitions
https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...
- CH5702 Count The Repetitions
题意 5702 Count The Repetitions 0x50「动态规划」例题 描述 定义 conn(s,n) 为 n 个字符串 s 首尾相接形成的字符串,例如: conn("abc& ...
- [LeetCode] Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- [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 Linked List Nodes【Naive】
Count how many nodes in a linked list. Example Given 1->3->5, return 3. 解法一: /** * Definition ...
- 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 ...
随机推荐
- ATL控件签名之后页面还提示“在此页面上的Activex控件和本页上的其他部分及交互可能不安全”
ATL控件正常签名打包,然后安装之后还是会提示: 没理由啊,签名是花钱搞得正当的签名.后来查了资料才知道这还不够,需要在创建ATL控件的时候继承一个IObjectSafetyImpl 类 知道了原因, ...
- Eclipse安装Freemarker Editor插件
在下面网址里下载freemarker-ide: http://sourceforge.net/projects/freemarker-ide/files/ 下载完成后后解压,由于该IDE里面的free ...
- 【325】python**:selenium
参考:selenium安装方式 参考:Selenium2(Webdriver)+Python处理浏览器弹窗
- Dubbo Overview
Overview Architecture Provider: 暴露服务的服务提供方. Consumer: 调用远程服务的服务消费方. Registry: 服务注册与发现的注册中心. Monitor: ...
- Unix高级编程Note2
[Unix Note2] 1.信号屏蔽 2.信号不会排队,即产生同时产生10次,会被合并为1次. 3.sigsuspend,sigsuspend后,进程就挂在那里,等待着开放的信号的唤醒.系统在接收到 ...
- Python dict() 函数
Python dict() 函数 Python 内置函数 描述 dict() 函数用于创建一个字典. 语法 dict 语法: class dict(**kwarg) class dict(mappi ...
- 表示集合的数据结构:数组(Array),对象(Object),Map和Set
Map和Set是ES6标准新增的数据类型 Map: 是一组键值对的结构,使用一个二维数组来初始化Map,例如: var m = new Map([['xiaohong',100],['xiaolan' ...
- 大楼轮廓 · building-outline
[抄题]: 水平面上有 N 座大楼,每座大楼都是矩阵的形状,可以用一个三元组表示 (start, end, height),分别代表其在x轴上的起点,终点和高度.大楼之间从远处看可能会重叠,求出 N ...
- JDeveloper 开发环境配置
JDeveloper 开发环境配置 程序员的基础教程:菜鸟程序员
- __imp___vsnprintf
unresolved external symbol __imp___vsnprintf 解决方案找到了. 在vs2015工程选项,链接器附加依赖项里面添加legacy_stdio_definitio ...