女孩的研究 (马拉车)

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!

//马拉车模板

#pragma warning(disable:4996)
#include"iostream"
#include"functional"
#include"algorithm"
#include"cstring"
#include"stack"
#include"cmath"
#include"queue"
#include"vector"
#include"map"
typedef long long int ll;
using namespace std;
const int maxn=110009;
char a[maxn*3+9],s[maxn*3+9],m;
int p[maxn*3+9];
int ans=0;
int len=0,id=0;
int cnt=0;
inline void solve(){
	s[cnt++]='$';
	s[cnt++]='#';
	for(int i=0;i<len;i++){
		 s[cnt++]=a[i];
		 s[cnt++]='#';
	}
	s[cnt]=0;
	int r=0,mid=0;
	for(int i=1;i<=cnt;i++){
		if(r>i) p[i]=min(p[2*mid-i],r-i);
        else p[i]=1;
	  // 	p[i]=r>i?min(p[2*mid-i],r-mid):1;
	   	while(s[i+p[i]]==s[i-p[i]]) p[i]++;
		if(i+p[i]>r) {
	     	r=i+p[i];
			mid=i;	
		}
		if(p[i]-1>ans){
			ans=p[i]-1;
			id=mid;
		}
		
	}
}
int main(){
    while(cin>>m>>a){
    	ans=cnt=len=0;
    	len=strlen(a);
    	if(!len) break;
		solve();
		if(ans>=2){
		   printf("%d %d\n",(id-ans+2)/2-1,(id-ans+2)/2-2+ans);
	       for(int i=(id-ans+2)/2-1;i<=(id-ans+2)/2-2+ans;i++){
	       	  putchar(((a[i]-'a')-(m-'a')+26)%26+'a');
		   }
		   cout<<endl;	   
	    }
		else{
			cout<<"No solution!"<<endl;
		} 
        
	}
} 
发布了49 篇原创文章 · 获赞 0 · 访问量 660

猜你喜欢

转载自blog.csdn.net/qq_14989799/article/details/105103650