HDU 3068 最长回文 Manacher

最长回文

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12328    Accepted Submission(s): 4535


Problem Description
给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.
回文就是正反读都是一样的字符串,如aba, abba等
 

Input
输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S
两组case之间由空行隔开(该空行不用处理)
字符串长度len <= 110000
 

Output
每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度.
 

Sample Input
   
   
   
   
aaaa abab
 

Sample Output
   
   
   
   
4 3
 

Source
2009 Multi-University Training Contest 16 - Host by NIT
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1358  1686  3336  3065  3746 
 


ACode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 110011
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
char str[maxn];
char ma[maxn<<1];
int mp[maxn<<1];
int ans;
void Manacher(){
    int l=0;ma[l++]='$';
    ma[l++]='#';ans=-INF;
    int m=strlen(str);
    FOR(i,0,m-1){
        ma[l++]=str[i];
        ma[l++]='#';
    }
    ma[l]=0;
    int mx=0,id=0;
    FOR(i,0,l-1){
        mp[i]=mx>i?min(mp[(id<<1)-i],mx-i):1;
        while(ma[i+mp[i]]==ma[i-mp[i]])mp[i]++;
        if(mp[i]+i>mx){
            mx=mp[i]+i;
            id=i;
        }
        ans=ans>mp[i]?ans:mp[i];
    }
    printf("%d\n",ans-1);
}
int main(){
    while(rds(str)!=EOF)
        Manacher();
    return 0;
}
/*
aaaa

abab
*/

你可能感兴趣的:(C++,HDU,manacer)