hdu 3294 Girls' research【manacher】

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <iomanip>

using namespace std;

const int N = 1100550;

int p[2 * N];//记录回文半径
char str0[N];//原始串
char str[2 * N];//转换后的串

void init()
{
int i, l;
str[0] = '@'; str[1] = '#';
for (i = 0, l = 2; str0[i]; i++, l += 2)
{
str[l] = str0[i];
str[l + 1] = '#';
}
str[l] = 0;
}

int solve()
{
int ans = 0;
int i, mx, id;
mx = 0;//mx即为当前计算回文串最右边字符的最大值
for (i = 1; str[i]; i++)
{
if (mx>i)
p[i] = p[2 * id - i]>(mx - i) ? (mx - i) : p[2 * id - i];
else
p[i] = 1;//如果i>=mx，要从头开始匹配
while (str[i + p[i]] == str[i - p[i]])
p[i]++;
if (i + p[i]>mx)//若新计算的回文串右端点位置大于mx，要更新po和mx的值
{
mx = i + p[i];
id = i;
}
ans = max(ans, p[i]);
}
return ans - 1;
}

char s1[N];

int main()
{
while (scanf("%s %s", s1,str0) != EOF)
{
int tmp = s1[0] - 'a';
int len = strlen(str0);
for (int i = 0;i < len;i++)
str0[i] = 'a' + ((str0[i] - 'a') - tmp + 26) % 26;

//puts(str0);

init();
int ans = solve();
if (ans < 2)
{
puts("No solution!");
continue;
}

int pos = 0;
for (int i = 1;str[i];i++)
{
if (p[i] - 1 == ans)
{
pos = i;
break;
}
}
int pos1 = (pos - ans + 1) / 2 - 1;
int pos2 = (pos + ans - 1) / 2 - 1;
printf("%d %d\n", pos1, pos2);
for (int i = pos1;i <= pos2;i++)
printf("%c", str0[i]);
printf("\n");

}
return 0;
}

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客