Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
count-the-repetitions.cpp 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2016-12-20 14:00 +08:00 . Update count-the-repetitions.cpp
// Time: O(s1 * min(s2, n1))
// Space: O(s2)
class Solution {
public:
int getMaxRepetitions(string s1, int n1, string s2, int n2) {
vector<int> repeatCount(s2.size() + 1, 0);
unordered_map<int, int> lookup;
int j = 0, count = 0;
for (int k = 1; k <= n1; ++k) {
for (int i = 0; i < s1.size(); ++i) {
if (s1[i] == s2[j]) {
j = (j + 1) % s2.size();
count += (j == 0);
}
}
if (lookup.find(j) != lookup.end()) { // cyclic
int i = lookup[j];
int prefixCount = repeatCount[i];
int patternCount = (count - repeatCount[i]) * ((n1 - i) / (k - i));
int suffixCount = repeatCount[i + (n1 - i) % (k - i)] - repeatCount[i];
return (prefixCount + patternCount + suffixCount) / n2;
}
lookup[j] = k;
repeatCount[k] = count;
}
return repeatCount[n1] / n2; // not cyclic iff n1 <= s2
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助