poj 3974 Palindrome (最长回文字串,manacher裸题)
poj3974 题意:求最大长度的回文字串。 思路:manacher裸题,用来练习算法。
/* ***********************************************
Author :111qqz
Created Time :2016年04月18日 星期一 16时32分25秒
File Name :code/poj/3974.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=2E6+7;
7char st[2*N];
8int p[2*N];
1int manacher(char *s)
2{
3 int len = strlen(s);
4 for ( int i = len ; i >= 0 ; i-- )
5 {
6 s[i+i+2]=s[i];
7 s[i+i+1]='#';
8 }
9 s[0]='$';
int id = 0 ;
int maxlen = 0 ;
1 for ( int i = 2 ; i < 2*len+1 ; i++)
2 {
3 if (id+p[id]>i) p[i] = min(p[2*id-i],id+p[id]-i);
4 else p[i] = 1;
1 while (s[i+p[i]]==s[i-p[i]]) p[i]++;
2 if (id+p[id]<i+p[i]) id = i;
3 if (p[i]>maxlen) maxlen = p[i];
4 }
5 return maxlen-1;
6}
7int main()
8{
9 #ifndef ONLINE_JUDGE
10 freopen("code/in.txt","r",stdin);
11 #endif
12 int cas = 0 ;
13 while (scanf("%s",st)!=EOF)
14 {
15 if (st[0]=='E'&&st[1]=='N'&&st[2]=='D') break;
16 printf("Case %d: %d\n",++cas,manacher(st));
}
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}