http://acm.hust.edu.cn/problem/show/1328
我太弱了,这个破next我理解了那么久,今天发现全不会了
//#define _TEST _TEST #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <cmath> #include <algorithm> using namespace std; /************************************************ Code By willinglive Blog:http://willinglive.cf ************************************************/ #define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++) #define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--) #define MS(arr,x) memset(arr,x,sizeof(arr)) #define LL long long #define INE(i,u,e) for(int i=head[u];~i;i=e[i].next) inline const int read() {int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1; for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;} ///////////////////////////////////////////////// char str[100010]; int next[100010]; int len; int ans[100010]; ///////////////////////////////////////////////// void get_next() { int i=0,j=-1; next[0]=-1; while(i<len) if(j==-1||str[i]==str[j]) next[++i]=++j; else j=next[j]; } ///////////////////////////////////////////////// void input() { MS(ans,0); scanf("%s",str); len=strlen(str); } void solve() { get_next(); LL all=0; rep(i,0,len-1) { ans[i]=ans[next[i+1]-1]; ans[i]++; all+=ans[i]; } printf("%lld\n",all); } ///////////////////////////////////////////////// int main() { #ifndef _TEST freopen("std.in","r",stdin); freopen("std.out","w",stdout); #endif for(int T=read();T--;) input(),solve(); return 0; }