比赛 NOIP2025模拟赛2 评测结果 AAAAAAAAAAAAAAAA
题目名称 回文块 最终得分 100
用户昵称 梦那边的美好ME 运行时间 0.425 s
代码语言 C++ 内存使用 8.99 MiB
提交时间 2025-11-25 08:50:59
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;

using ll=unsigned long long;
const ll B=131;

ll T,n;
char s[5100000];
ll ans;
ll h1[5100000],h2[5100000],p[5100000];

ll gh1(int l,int r){
    return h1[r+1]-h1[l]*p[r-l+1];
}

ll gh2(int l,int r){
    return h2[l]-h2[r+1]*p[r-l+1];
}

int main(){
	freopen("palin.in","r",stdin);
	freopen("palin.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>T;
    while(T--){
        cin>>s;
        n=strlen(s);
        if(n==0){
            cout<<"0\n";
            continue;
        }
        h1[0]=0;
        for(int i=0;i<n;i++){
            h1[i+1]=h1[i]*B+s[i];
        }
        h2[n]=0;
        for(int i=n-1;i>=0;i--){
            h2[i]=h2[i+1]*B+s[i];
        }
        p[0]=1;
        for(int i=1;i<=n;i++){
            p[i]=p[i-1]*B;
        }
        ans=0;
        ll l=0,r=n-1;
        while(l<=r){
            if(l==r){
                ans++;
                break;
            }
            ll mid=(r-l+1)/2;
            ll fl=-1;
            for(int len=1;len<=mid;len++){
                ll lh=gh1(l,l+len-1);
                ll rh=gh1(r-len+1,r);
                if(lh==rh){
                    fl=len;
                    break;
                }
            }
            if(fl!=-1){
                ans+=2;
                l+=fl;
                r-=fl;
            }else{
                ans++;
                break;
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}