一种在 kmp 的同时对所有前缀数 border 的方法
提供一个可以
考虑经典结论:border 的 border 是 border。这意味着,当我们用 KMP 算出
进一步容易发现由于
特别地,需要注意
int tc;
string s;
int kmp[N],cnt[N];
int main(){
#ifdef Ltp
freopen("hack.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin>>tc;
fr1(i,1,tc){
cout<<"Case "<<i<<": ";
cin>>s;
s='@'+s;
int n=s.length()-1;
int poi=0;
fr1(i,2,n){
while(poi&&s[poi+1]!=s[i]) poi=kmp[poi];
if(s[poi+1]==s[i]) poi++;
kmp[i]=poi;
if(!poi) cnt[i]=0;
else cnt[i]=cnt[poi]+1;
}
cout<<cnt[n]<<'\n';
}
ET;
}
//ALL FOR Zhang Junhao.