I Isomorphic Inversion
题目描述
Let s be a given string of up to 106 digits. Find the maximal k for which it is possible to partition s into k consecutive contiguous substrings, such that the k parts form a palindrome.
More precisely, we say that strings s0, s1, . . . , sk−1 form a palindrome if si = sk−1−i for all 0 ≤ i < k.
In the first sample case, we can split the string 652526 into 4 parts as 6|52|52|6, and these parts together form a palindrome. It turns out that it is impossible to split this input into more than 4 parts while still making sure the parts form a palindrome.
输入
A nonempty string of up to 106 digits.
输出
Print the maximal value of k on a single line.
样例输入
132594414896459441321
样例输出
9
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull has[1000010], base[1000010];
const int b = 311;///hash 基数 或131
char str[1000010];
int main()
{
ios;
cin>>str+1;
int len=strlen(str+1);
base[0]=1;
for(int i=1;i<=len+2;i++)
{
base[i]=base[i-1]*b;
}
has[0]=0;
for(int i=1;i<=len;i++)
{
has[i]=has[i-1]*b+(str[i]-'0'+1);
}
ull s,e;
ll ans=0;
int lasts=1,laste=len;
for(int i=1,j=len;i<=len/2;i++,j--)
{
s=has[i]-has[lasts-1]*base[i-lasts+1];
e=has[laste]-has[j-1]*base[laste+1-j];
if(s==e)
{
ans+=2;
lasts=i+1;
laste=j-1;
}
}
if(len%2==0&&lasts>laste)cout<<ans<<endl;
else cout<<ans+1<<endl;
return 0;
}