P7114

kmkmp

#include<bits/stdc++.h>
using namespace std;
char str[1500005];
int n,kmp[1500005],cir[1500005],tms[30],suf[1500005],pre[2500005],per[27],cnt;
long long ans;
void deal() {
	char c=getchar();
	ans=0ll;
	memset(kmp,0,sizeof(kmp));
	memset(cir,0,sizeof(cir));
	memset(tms,0,sizeof(tms));
	memset(suf,0,sizeof(suf));
	memset(pre,0,sizeof(pre));
	memset(per,0,sizeof(per));
	while(c>'z'||c<'a')	c=getchar();
	n=0;
	int last;
	while(c<='z'&&c>='a') {
		str[++n]=c;
		c=getchar();
	}
	for(int i=2;i<=n;i++) {
		last=kmp[i-1];
		while(str[i]!=str[last+1]&&last!=0)last=kmp[last];
		if(str[i]==str[last+1])kmp[i]=last+1;
		else kmp[i]=0;
	}
	cnt=0;
	for(int i=1;i<=26;i++)tms[i]=0;
	for(int i=n;i>=1;i--) {
		tms[str[i]-'a'+1]++;
		if(tms[str[i]-'a'+1]&1)suf[i]=suf[i+1]+1;
		else suf[i]=suf[i+1]-1;
	}
	for(int i=1;i<=26;i++)tms[i]=0;
	for(int i=1;i<=n;i++) {
		tms[str[i]-'a'+1]++;
		if(tms[str[i]-'a'+1]&1)pre[i]=pre[i-1]+1;
		else pre[i]=pre[i-1]-1;
	}
	for(int i=1;i<n;i++) {
		if(i>=2) {
			ans+=per[suf[i+1]];
			for(int j=i+i;j<n;j+=i) {
				if(((i%(j-kmp[j]))==0)&&((j/(j-kmp[j]))>1)) {
					ans+=(long long)per[suf[j+1]];
				}else break;
			}
		}
		for(int j=1;j<pre[i];j++)per[j]=per[j];
		for(int j=pre[i];j<=26;j++)per[j]=per[j]+1;
	}
	printf("%lld\n",ans);
}
int main() {
int T;
	scanf("%d\n",&T);
	for(;T>0;T--) {deal();}
	return 0;
}
posted @ 2024-10-22 15:44  yzc_is_SadBee  阅读(5)  评论(0编辑  收藏  举报