【HDU】3068 最长回文

【算法】manacher

【题解】【算法】字符串

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=300010;
int n,p[maxn];
char s[maxn],ss[maxn];
void manacher()
{
    int mx=0,id=0;
    int ans=0;
    for(int i=2;i<=n;i++)//$不管 
     {
         if(i<mx)
          {
              p[i]=min(p[id*2-i],mx-i+1);
          }
         else p[i]=1;
         while(s[i+p[i]]==s[i-p[i]])p[i]++;
         if(i+p[i]-1>mx)
          {
              mx=i+p[i]-1;
              id=i;
          }
         ans=max(ans,p[i]-1);
     }
    printf("%d
",ans);
}
int main()
{
    while(scanf("%s",ss+1)==1)
     {
         int tot=strlen(ss+1);
        n=2;s[1]='$';s[2]='#';
        for(int i=1;i<=tot;i++)s[++n]=ss[i],s[++n]='#';
        manacher();
     }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/onioncyc/p/6670173.html