题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3294
给出一个字符串和加密的字符规律
例如 c abcba
c代表把串中的c改成a,d改成b... b改成z,a改成y...
即上串是yzazy,然后求出它的最长回文子串, 并记录子串的开始下标和结束下标就行了;
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N = 4e5+; int p[N], id, mx, ans, Index;
char s[N], ch[], s0[N]; void Manacher(char s[], int n)
{
id = mx = ans = ;
for(int i=; i<n; i++)
{
if(mx>i)
p[i] = min(p[id*-i], mx-i);
else
p[i] = ;
while(s[i-p[i]] == s[i+p[i]])
p[i]++;
if(p[i]+i>mx)
{
mx = p[i]+i;
id = i;
}
if(p[i]>ans)
{
ans = p[i];
Index = i;///记录结果的中心下标;
}
}
} int main()
{
while(scanf("%s%s", ch, s)!=EOF)
{
int len = strlen(s);
int k = ch[]-'a';
for(int i=; i<len; i++)///转化成真正的串s;
{
if(s[i]-k<'a')
s[i]=s[i]-k+;
else
s[i]=s[i]-k;
}
///printf("%s\n", s);
for(int i=len; i>=; i--)
{
s[i+i+] = s[i];
s[i+i+] = '#';
}
s[] = '$';
Manacher(s, *len+);
ans = ans-;
k = ans/;///前面或后面有几个字母在回文串中;
if(k == )
{
printf("No solution!\n");
continue;
}
int L = Index-ans+;
int R = Index+ans-;
printf("%d %d\n", (L-)/, (R-)/);
for(int i=L; i<=R; i++)
if(s[i]!='#')
printf("%c", s[i]);
printf("\n");
}
return ;
}