1. Title link:
Calculation repetition
2. The main idea of the topic:
The topic is simple and easy to understand
3. Analysis:
The problem requires the largest m so that conn(s2, n2 * m) is a subsequence of conn(s1, n1).
It is equivalent to finding the largest h such that conn(s2, h) is a subsequence of conn(s1, n1)
It is easy to know, m = h / n2.
The first idea is a binary answer, but check is not easy to write, so it can be solved by preprocessing + multiplication.
Because when multiplying the enumeration k, if we can get it together, we need to know how far we should jump from the current point s1[p]
Therefore, f[i][j] can be preprocessed: starting from s1[i], the minimum number of walks can contain 2^j s2.
It's a bit messy, see the code for details.
4. Code implementation:
/**
状态表示 f[i][j]: 从 s1[i] 开始,最少要走多少步,才能包含 2^j 个 s2
状态计算 f[i][j] = f[i][j - 1] + f[i + f[i][j - 1]][j - 1], j > 0
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = (int)1e2;
int n1, n2;
char s1[M + 5], s2[M + 5];
ll f[M + 5][27];
ll work()
{
int len1 = strlen(s1);
int len2 = strlen(s2);
for(int i = 0; i < len1; ++i)
{
f[i][0] = 0;
int p = i, c = 0;
for(int j = 0; j < len2; ++j)
{
while(s1[p] != s2[j])
{
p = (p + 1) % len1;
++c;
if(c == len1)
{
return 0;
}
}
f[i][0] += c + 1;
c = 0;
p = (p + 1) % len1;
}
}
for(int j = 1; j <= 26; ++j)
{
for(int i = 0; i < len1; ++i)
{
f[i][j] = f[i][j - 1] + f[(i + f[i][j - 1]) % len1][j - 1];
}
}
ll p = 0, m = 0;
for(int k = 26; k >= 0; --k)
{
if(p + f[p % len1][k] <= n1 * len1)
{
m |= (1<<k);
p += f[p % len1][k];
}
}
return m / n2;
}
int main()
{
// freopen("input.txt", "r", stdin);
while(~scanf("%s %d %s %d", s2, &n2, s1, &n1))
{
printf("%lld\n", work());
}
return 0;
}