标签:acm mode eve color include follow ide isp 技术
1 #include <cctype> 2 #include <cstdio> 3 4 const int mod=10007; 5 const int MAXN=10000010; 6 7 int n,ans,T; 8 9 int next[MAXN],dp[MAXN]; 10 11 char s[MAXN]; 12 13 inline void read(int&x) { 14 int f=1;register char c=getchar(); 15 for(x=0;!isdigit(c);c==‘-‘&&(f=-1),c=getchar()); 16 for(;isdigit(c);x=x*10+c-48,c=getchar()); 17 x=x*f; 18 } 19 20 inline void KMP_next() { 21 for(int i=1;i<=n;++i) next[i]=0; 22 next[1]=0; 23 int j=0; 24 for(int i=2;i<=n;i++){ 25 while(j>0&&s[j+1]!=s[i]) j=next[j]; 26 if(s[j+1]==s[i]) j+=1; 27 next[i]=j; 28 } 29 } 30 31 int hh() { 32 freopen("ha.in","r",stdin); 33 freopen("ha.out","w",stdout); 34 read(T); 35 while(T--) { 36 read(n); 37 scanf("%s",s+1); 38 KMP_next(); 39 ans=0; 40 for(int i=1;i<=n;++i) { 41 dp[i]=(dp[next[i]]+1)%mod; 42 ans=(ans+dp[i])%mod; 43 } 44 printf("%d\n",ans); 45 } 46 fclose(stdin); 47 fclose(stdout); 48 return 0; 49 } 50 51 int sb=hh(); 52 int main(int argc,char**argv) {;}
HDU 3336 Count the string (kmp+dp)
标签:acm mode eve color include follow ide isp 技术
原文地址:http://www.cnblogs.com/whistle13326/p/7605959.html