正题
题目大意
要求s2n2∗ms_2^{{n_2}*m}s2n2∗m是串s1n1s_1^{n_1}s1n1的字串,求最大的mmm
解题思路
首先求一个m′m'm′使得s2ms_2^ms2m能够被s1n1s_1^{n_1}s1n1生成,然后可以从而求出mmm
倍增优化,设fi,jf_{i,j}fi,j表示从s1is1_is1i开始至少需要多少字符串才能生成s22js_2^{2^j}s22j
然后有
fi,j=fi,j−1+f(i+fi,j−1)%∣s1∣,j−1f_{i,j}=f_{i,j-1}+f_{(i+f_{i,j-1})\%|s1|,j-1}fi,j=fi,j−1+f(i+fi,j−1)%∣s1∣,j−1
然后就拼凑
codecodecode
#include<cstdio>
#include<algorithm>
#include<string>
#include<iostream>
#include<cstring>
#define ll long long
using namespace std;
ll n1,n2,f[105][32];
string s1,s2;
void VeryImportantFakeMainP()
{memset(f,0,sizeof(f));for(ll i=0;i<s1.size();i++){ll pos=i;f[i][0]=0;for(ll j=0;j<s2.size();j++){ll cnt=0;while(s1[pos]!=s2[j]){pos=(pos+1)%s1.size();if(++cnt>=s1.size()){printf("0\n");return;}}pos=(pos+1)%s1.size();f[i][0]+=cnt+1;}}for(ll j=1;j<=30;j++)for(ll i=0;i<s1.size();i++)f[i][j]=f[i][j-1]+f[(i+f[i][j-1])%s1.size()][j-1];ll m=0;for(ll st=0;st<s1.size();st++){ll x=st,ans=0;for(ll k=30;k>=0;k--)if(x+f[x%s1.size()][k]<=s1.size()*n1){x+=f[x%s1.size()][k];ans+=1<<k;}m=max(m,ans);}printf("%lld\n",m/n2);
}
int main()
{while(cin>>s2>>n2>>s1>>n1){VeryImportantFakeMainP();}
}