比赛 NOIP2025模拟赛2 评测结果 AAAAAAAAAAAAAAAA
题目名称 回文块 最终得分 100
用户昵称 郑霁桓 运行时间 1.365 s
代码语言 C++ 内存使用 43.76 MiB
提交时间 2025-11-25 08:35:47
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
int n,m,pl,pr,l,r,as;
string str;
unsigned long long hs[5000005],a2[5000005];
const unsigned long long P=27;
unsigned long long f(int ll,int rr){
	return hs[rr]-hs[ll-1]*a2[rr-ll+1];
} 
int main(){
	freopen("palin.in","r",stdin);
	freopen("palin.out","w",stdout);
	cin>>n;
	a2[0]=1;
	for(int i=1;i<=5000000;i++){
		a2[i]=a2[i-1]*P; 
	}
	for(int i=1;i<=n;i++){
		cin>>str;
		m=str.size();
		as=0;
		for(int j=1;j<=m;j++){
			hs[j]=hs[j-1]*P+str[j-1]-'a'+1;
		}
		l=pl=1,r=pr=m;
		while(l<r){
			if(f(pl,l)==f(r,pr)) as+=2,pl=l+1,pr=r-1;
			l++,r--;
		}
		if(pl<=pr) as++;
		cout<<as<<"\n";
	}
	return 0;
}