标签:字符串
Problem Description
One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps:
First step: girls will write a long string (only contains lower case) on the paper. For example, “abcde”, but ‘a’ inside is not the real ‘a’, that means if we define the ‘b’ is the real ‘a’, then we can infer that ‘c’ is the real ‘b’, ‘d’ is the real ‘c’ ……, ‘a’ is the real ‘z’. According to this, string “abcde” changes to “bcdef”.
Second step: girls will find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2.
Input
Input contains multiple cases.
Each case contains two parts, a character and a string, they are separated by one space, the character representing the real ‘a’ is and the length of the string will not exceed 200000.All input must be lowercase.
If the length of string is len, it is marked from 0 to len-1.
Output
Please execute the operation following the two steps.
If you find one, output the start position and end position of palindromic string in a line, next line output the real palindromic string, or output “No solution!”.
If there are several answers available, please choose the string which first appears.
Sample Input
b babd a abcd
Sample Output
0 2 aza No solution!
Author
wangjing1111
Source
2010 “HDU-Sailormoon” Programming Contest
Recommend
lcy | We have carefully selected several similar problems for you: 3293 3288 3295 3292 3291
manacher解决即可
/*************************************************************************
> File Name: hdu3294.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年02月18日 星期三 16时47分21秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
//const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
const int N = 201010;
char str[N << 1];
char S[N];
int p[N << 1];
char ch[10];
void manacher (int cnt)
{
memset (p, 0, sizeof(p));
int MaxId = 0;
int id;
for (int i = 1; i < cnt; ++i)
{
if (MaxId > i)
{
p[i] = min (p[2 * id - i], MaxId - i);
}
else
{
p[i] = 1;
}
while (str[i + p[i]] == str[i - p[i]])
{
++p[i];
}
if (p[i] + i > MaxId)
{
MaxId = p[i] + i;
id = i;
}
}
}
int main ()
{
while (~scanf("%s%s", ch, S))
{
int len = strlen (S);
for (int i = 0; i < len; ++i)
{
S[i] = (S[i] - ch[0] + 26) % 26 + ‘a‘;
}
str[0] = ‘?‘;
str[1] = ‘#‘;
int cnt = 2;
for (int i = 0; i < len; ++i)
{
str[cnt++] = S[i];
str[cnt++] = ‘#‘;
}
str[cnt] = ‘*‘;
manacher(cnt);
int s, size = 0;
for (int i = 1; i < cnt; ++i)
{
int tmp = p[i] - 1;
if (tmp < 2)
{
continue;
}
int tmps;
if (str[i] != ‘#‘)
{
tmps = i / 2 - 1 - (tmp - 1) / 2;
}
else
{
tmps = i / 2 - (tmp / 2);
}
if (tmp > size)
{
size = tmp;
s = tmps;
}
else if (tmp == size && s > tmps)
{
s = tmps;
}
}
if (size < 2)
{
printf("No solution!\n");
}
else
{
printf("%d %d\n", s, s + size - 1);
for (int i = s; i <= s + size - 1; ++i)
{
printf("%c", S[i]);
}
printf("\n");
}
}
return 0;
}
标签:字符串
原文地址:http://blog.csdn.net/guard_mine/article/details/43877359