提供一个可以 数出所有前缀 border 数量的方法。感觉还是有用的!
考虑经典结论:border 的 border 是 border。这意味着,当我们用 KMP 算出 的最长 border 的时候,我们可以继承 的所有 border。
进一步容易发现由于 是最长的,所以我们恰好会增多一个 border。于是可以 转移。
特别地,需要注意 没有 border 的边界情况。
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.