$KMP$。
枚举每一个后缀,去原串中进行匹配,每次匹配到原串到$i$位置的后缀与这次枚举的后缀的前缀,更新答案。
#include<bits/stdc++.h> using namespace std; int T; char a[2010]; int len1,len2; int nx[2010]; long long ans; void getNext(int x) { int j, k; j = x; k = x-1; nx[x] = x-1; while(j < len1) if(k == x-1 || a[j] == a[k]) nx[++j] = ++k; else k = nx[k]; } void KMP_Index(int x) { int i = 0, j = x; getNext(x); while(i < len1 ) { if(j == x-1 || a[i] == a[j]) { if(j!=x-1) { long long B = (long long)(j-x+1); long long A = (long long)(i+1-B); long long C = (long long)(len2-B); ans=ans^(A*B*B*C); } i++; j++; } else j = nx[j]; } } int main() { scanf("%d",&T); while(T--) { scanf("%s",a); len1 = strlen(a); ans=0; for(int i=0; i<len1; i++) { len2 = len1-i; memset(nx,0,sizeof nx); KMP_Index(i); } printf("%lld\n",ans); } return 0; }