/*author:revolIA*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e7+1e6+7;
char s[maxn+1<<1];
int Len[maxn+1<<1];
int manacher(char str[],int n){
Len[0] = 1;
int R = 0,mid = 0,ans = 0;
for(int i=1 ; i<=n ; i++){
Len[i] = R > i ? min(R-i,Len[(mid<<1)-i]) : 1 ;
while(str[i + Len[i]] == str[i - Len[i]])
Len[i]++;
if(i + Len[i] > mid + Len[mid]){
mid = i;
R = i + Len[i];
}
ans = max(ans,Len[i]);
}
return ans - 1;
}
int main(){
while(~scanf("%s",s)){
int len = strlen(s);
for(int i=len;i>=0;i--)
s[i<<1|1] = '#',s[i+1<<1] = s[i];
s[0] = '$';
printf("%d\n",manacher(s,len<<1|1));
}
return 0;
}
自己yy的一个想法,需要算两遍、、
/*author:revolIA*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5+7;
char s[maxn+1<<1]="abaccdcc",t[maxn+1<<1];
int Len[maxn+1<<1],End[maxn+1<<1];
int manacher(char str[],int n){
memset(Len,0,sizeof Len);
memset(End,0,sizeof End);
Len[0] = 1;
int R = 0,mid = 0,ans = min(n>>1,2);
if(ans == 1)ans = 0;
for(int i=1 ; i<=n ; i++){
Len[i] = R > i ? min(R-i,Len[(mid<<1)-i]) : 1;
while(str[i + Len[i]] == str[i - Len[i]]){
Len[i]++;
if(!End[i+Len[i]-1])End[i+Len[i]-1] = i;
}
if(i + Len[i] > mid + Len[mid]){
mid = i;
R = i + Len[i];
}
if(!End[i])End[i] = mid;
}
//for(int i=1;i<=n;i++)printf("%d %d\n",i,End[i]);
for(int i=1;i<=n;i++)if(s[i] == '#'){
int j = (End[i]<<1)-i;
int k = (End[j]<<1)-j;
if(j>0&&k>0){
int lenj = (i-j+1)>>1;
int lenk = (j-k+1)>>1;
if(!lenk)continue;
//printf("[%d,%d]+[%d,%d]-->%d %d\n",k,j,j,i,lenk,lenj);
ans = max(ans,lenj+lenk);
}
}
return ans;
}
void init(int n){
for(int i=n;i>=0;i--){
s[i<<1|1] = '#',s[i+1<<1] = s[i];
t[i<<1|1] = '#',t[i+1<<1] = t[i];
}
s[0] = t[0] = '$';
}
/*void debug(){
puts(s);
for(int i=0;s[i];i++)printf("%d",i%10);
puts("");
}
bool jude(int l,int r){
while(l<=r){
if(s[l]!=s[r])
return false;
l++,r--;
}
return true;
}*/
int main(){
//freopen("input.txt","r",stdin);
//freopen("outWa.txt","w",stdout);
int T = 1;
//scanf("%d",&T);
while(T--){
scanf("%s",s);
int n = strlen(s);
for(int i=0;i<n;i++)t[i] = s[n-1-i];
init(n);
//debug();
printf("%d\n",max(manacher(s,n<<1|1),manacher(t,n<<1|1)));
}
return 0;
}
简洁的题解想法、、
/*author:revolIA*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5+7;
char s[maxn+1<<1]="abaccdcc";
int Len[maxn+1<<1],End[maxn+1<<1],Start[maxn+1<<1];
int L[maxn+1<<1],R[maxn+1<<1];
int manacher(char str[],int n){
Len[0] = 1;
int R = 0,mid = 0,ans = 0;
for(int i=1 ; i<=n ; i++){
Len[i] = R > i ? min(R-i,Len[(mid<<1)-i]) : 1;
while(str[i + Len[i]] == str[i - Len[i]]){
Len[i]++;
}
if(i + Len[i] > mid + Len[mid]){
mid = i;
R = i + Len[i];
}
End[i+Len[i]-1] = max(End[i+Len[i]-1],Len[i]-1);
Start[i-Len[i]+1] = max(Start[i-Len[i]+1],Len[i]-1);
}
for(int i = 1;i <= n;i += 2)Start[i] = max(Start[i],Start[i-2]-2);
for(int i = n;i >= 1;i -= 2)End[i] = max(End[i],End[i+2]-2);
//for(int i=1;i<=n;i++)printf("%d %d\n",Start[i],End[i]);
for(int i = 1;i <= n;i += 2)if(End[i] && Start[i]){
ans = max(ans,Start[i]+End[i]);
}
return ans;
}
void init(int n){
for(int i=n;i>=0;i--)
s[i<<1|1] = '#',s[i+1<<1] = s[i];
s[0] = '$';
}
int main(){
scanf("%s",s);
int n = strlen(s);
init(n);
printf("%d\n",manacher(s,n<<1|1));
return 0;
}