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的更多相关文章

  1. 第七周 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个 ...

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

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

  3. 【leetcode 字符串】466. Count The Repetitions

    https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...

  4. CH5702 Count The Repetitions

    题意 5702 Count The Repetitions 0x50「动态规划」例题 描述 定义 conn(s,n) 为 n 个字符串 s 首尾相接形成的字符串,例如: conn("abc& ...

  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. [Swift]LeetCode466. 统计重复个数 | Count The Repetitions

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

  7. 466. Count Linked List Nodes【Naive】

    Count how many nodes in a linked list. Example Given 1->3->5, return 3. 解法一: /** * Definition ...

  8. Leetcode: Count The Repetitions

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

  9. 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 ...

随机推荐

  1. BTN事件的响应区域

    是其下所有小图片的区域组合.是并集

  2. Python Geoip 获取IP地址经度、纬度

    简介: 除了一些免费的 API 接口,例如 http://ipinfo.io/223.155.166.172 可以得到一些信息外,还可以通过 python-geoip 库来解决这个问题. shell ...

  3. class.__subclasses__()

    [class.__subclasses__()] Each class keeps a list of weak references to its immediate subclasses. Thi ...

  4. Unix高级编程Note3

    [Unix高级编程Note3] 1.RECURSIVE锁可以递归,普通锁只会死锁 2.线程安全函数 3.线程私有数据 4.pthread_once 5.线程取消点 6.线程信号 7.pread 8.d ...

  5. Thread.yield()的简单理解

    Thread.yield( )方法: 使当前线程从执行状态(运行状态)变为可执行态(就绪状态).cpu会从众多的可执行态里选择. 也就是说,当前也就是刚刚的那个线程还是有可能会被再次执行到的,并不是说 ...

  6. Python3 hex() 函数

    Python3 hex() 函数  Python3 内置函数 描述 hex() 函数用于将一个指定数字转换为 16 进制数. 语法 hex 语法: hex(x) 参数说明: x -- 一个整数 返回值 ...

  7. leetcode 235 236 二叉树两个节点的最近公共祖先

    描述: 给定二叉树两个节点,求其最近公共祖先.最近即所有公共祖先中深度最深的. ps:自身也算自身的祖先. 235题解决: 这是二叉搜索树,有序的,左边小右边大. TreeNode* lowestCo ...

  8. DSOframer 的简单介绍和资源整理(2015-09-02重新整理)

    DSOframer 是微软提供一款开源的用于在线编辑 Word. Excel .PowerPoint 的 ActiveX 控件.国内很多著名的 OA 中间件,电子印章,签名留痕等大多数是依此改进而来的 ...

  9. Luogu 4449 于神之怒加强版

    挺套路的题,然而一开始还是想错了…… $\sum_{i = 1}^{n}\sum_{j = 1}^{m}gcd(i, j) ^ {k} = \sum_{T = 1}^{min(n, m)}\left ...

  10. nignx重启

    .进入nginx安装目录sbin下 .输入./nginx -s reload