您的位置:首页 > 其它

hdu3294 Girls' research 回文字符串

2014-10-05 19:31 405 查看


Girls' research

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 540 Accepted Submission(s): 200



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!




题意:给出的所有字符都是小写的。然后给一个字符表示这个字符表示'a',依次类推假如这个字符是'b',所以接下来的c就表示b,d->c。然后z-a。然后给你一个字符串求出最长子串的起始位置和终点位置。然后再以前面这个规则输出这个子串,若有若干个同样长度的子串优先前面位置。还是老规矩马拉车算法。然后转换一下字符就可以了。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
using namespace std;
char c,s1[200010],s[400100];
int d[400100];
int main()
{
	while (~scanf("%c %s",&c,&s1))
	{
		getchar();
		int l=strlen(s1);
		int i,j=0;
		for (i=0;i<l;i++)
		{
			s[j++]='#';
			s[j++]=s1[i];
		}
		s[j++]='#';
		s[j++]='\0';
		l=strlen(s);
		int ans=0,mx=0,id,pos;
		memset(d,0,sizeof(d));
		for (i=0;i<l;i++)
		{
			if (mx>i)
				d[i]=min(d[2*id-i],mx-i);
			else 
				d[i]=1;
			while (i-d[i]>=0 && i+d[i]<l && s[i-d[i]]==s[i+d[i]]) d[i]++;
			if (d[i]+i>mx)
			{
				mx=d[i]+i;
				id=i;
			}
			if (d[i]>ans)
			{
				ans=d[i];
				pos=id;
			}
		}
		ans--;
		int k=c-'a';
		if (ans<2) cout<<"No solution!"<<endl;
		else 
		{
		//	if (ans % 2==0)
				printf("%d %d\n",(pos-ans)/2,(pos+ans-2)/2);
		//	else
		//		printf("%d %d\n",)
			for (i=pos-ans;i<pos+ans+1;i++)
			{
				if (s[i]=='#') continue;
				int f=s[i]-'a'-k;
				if (f<0) f=97+26+f;
				else f+=97;  
				printf("%c",f);
			}
			cout<<endl;
		}
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: