Question:
Given a string (L <= 100 W) with a length of L, evaluate an num array. Num [I] indicates the number of string s' In the prefix with the length of I, S is both the prefix and the suffix of the prefix, and | s '| * 2 <= I
Evaluate round (Num [I] + 1) % 1000000007
This is a deformation of the KMP algorithm... First, find the next array. By the way, find the CNT array, which indicates that after the I prefix is fixed = next [Fix] several times, it will get 0 and then re-match it, note that when fix * 2> I, make fix = next [Fix ].
Do not use the num array instead of next.
Do not forget to enable long
This question has passed ....
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 1000100using namespace std;typedef long long ll;ll ans;char s[M];int next[M],cnt[M];void KMP(){ int i,fix=0; cnt[1]=1; for(i=2;s[i];i++) { while( fix && s[fix+1]!=s[i] ) fix=next[fix]; if(s[fix+1]==s[i]) fix++; next[i]=fix; cnt[i]=cnt[fix]+1; } fix=0;ans=1; for(i=2;s[i];i++) { while( fix && s[fix+1]!=s[i] ) fix=next[fix]; if(s[fix+1]==s[i]) fix++; while( (fix<<1) > i ) fix=next[fix]; ans*=cnt[fix]+1; ans%=1000000007; }}int main(){ int T; //freopen("zoo.in","r",stdin); //freopen("zoo.out","w",stdout); for(cin>>T;T;T--) { scanf("%s",s+1); KMP(); cout<<ans<<endl; } }
Bzoj 3670 noi2014 zoo KMP Algorithm