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 ...
随机推荐
- eShopOnContainer 第一步
运行结果截图: 操作流程: 1. git上clone 项目 windows版的docker并且安装成功,配置3核CPU,4G内存 vs 2017 (15.5)版本以上. 打开项目 eshopOnCon ...
- linux 查看文件夹文件大小数目等信息
1. 查看当前目录所有文件和文件夹的大小 方法一: $du -sh * 或 $du -h -d 0 * '-d 0' 代表查询目录的深度为0 ,也就是当前目录,'-d 3' 表示文件目录深度为3,可以 ...
- linux 安装php7 Nginx
这里 记录下 本屌安装linux 下安装php7 即遇到的问题. wget http://cn2.php.NET/distributions/php-7.0.4.tar.gz tar zxvf ph ...
- sqlserver 必须声明标量变量 "***"。
发现在navicat premium上执行报这个异常,在sqlserver上不报,想到我之前的文章用存储过程时mysql里有个分割符,去掉“:”果然执行成功. DECLARE @countlmc IN ...
- 第一个独特字符位置 · first position unique character
[抄题]: 给出一个字符串.找到字符串中第一个不重复的字符然后返回它的下标.如果不存在这样的字符,返回 -1. 给出字符串 s = "lintcode",返回 0.给出字符串 s ...
- Docker RestApi Create mysql Container
docker 提供了创建容器的rest api http://192.168.150.6:8888/containers/create?name=demo-mysql docker mysql容器的参 ...
- 6-查看centos中的用户和用户组
转载自:http://www.cnblogs.com/ermao0423/p/9510636.html 查看centos中的用户和用户组 1.用户列表文件:/etc/passwd/ 2.用户组列表文件 ...
- Windows c++程序的基本结构
Windows c++程序的基本结构 1.一个完整的Windows应用程序通常由五种类型的文件组成 C语言源程序文件 头文件 模块定义文件 资源描述文件 项目文件 2.Windows应用程序构成基本框 ...
- [SQL]查询数据库中具有某个字段名的表
SELECT t.name AS table_name, c.name AS column_name FROM XOIFundData.sys.tables AS t INNER JOIN XOIFu ...
- Java设计模式(6)——建造者模式
定义:Builder模式也叫建造者模式或者生成器模式,Builder模式是一种对象创建型模式之一,用来隐藏复合对象(对象的属性为另一个对象的引用)的创建过程,它把复合对象的创建过程交给Builder, ...