POJ 3974 Palindrome solving report
manacher algorithm for maximum palindrome string of
problem-solving ideas: manacher algorithm. See above link will be able to understand, pay attention to the link in the code has the wrong place, do not directly copy, change yourself down on the line.
#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<fstream>
#include<iomanip>
#pragma warning(disable:4996)
#define INF 0x3f3f3f3f
#define ll long long
#define PI acos(-1.0)
const int N = 1000010;
const int maxn = 1e9;
using namespace std;
char str[2000005];
char a[1000005];
int len[2000005];
int main()
{
int cnt = 0;
while (scanf("%s",&a))
{
cnt++;
if (strcmp(a, "END") == 0)
break;
str[0] = '@';//头和尾的字符都要初始化,且初始化为不一样的字符,防止越界
int l = strlen(a);
for (int i = 1; i <= 2 * l; i += 2)
{
str[i] = '#';
str[i + 1] = a[i / 2];
}
str[2 * l + 1] = '#';
str[2 * l + 2] = '!';//初始化尾部
int rmax = 0, ans = 1, mid = 0;
for (int i = 1; i <= l*2; i++)
{
if (rmax > i)
len[i] = min(rmax - i, len[2 * mid - i]);//j+i=2*mid,len[i]在len[j]和rmax-i中取小
else
len[i] = 1;//如果不在已知回文串内,就要重新计算
while (str[i - len[i]] == str[i + len[i]])
len[i]++;
if (i + len[i] > rmax)
{
rmax = i + len[i];//更新已知右边界
mid = i;
}
ans = max(ans, len[i]-1);
}
printf("Case %d: %d\n", cnt,ans);
}
}