Girls' research

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,让你递推出这串字符真正的字符串是什么,然后叫你找出字符串里面回文串的开始与结束下标,然后打印出这串回文


个人觉得主要两种思路,一种先判断是否含有回文,再转换,再求出有回文的字符串里的回文串的开始下标和结束下标,和这串回文;

第二种是先转换,再判断是否有回文串,再判断这串回文的开始下标,结束下标,以及这串回文。

个人觉得第二种思路比较好


再求回文串的时候的主要方法是manacher算法

这个我也有总结过一下:点击打开链接,还有我看过的个人觉得总结的比较好的博客:点击打开链接


以下是代码:

#include 
#include 
#include
char a[1000005];
char b[1000005];
int p[1000005];
int min(int c,int d){
    if(c>d) return d;
    else return c;
}
int main()
{
    int i,j,k;
    char n;
    int len;
    int mx,id,t,max;
    while(scanf("%c",&n)==1){
        scanf("%s",b);
        len=strlen(b);
        for(i=0;i=n){
                    b[i]=97+(b[i]-n);//把比b[i]大于等于的转化
                }
                else{
                    b[i]=123-(n-b[i]);//把比b[i]小的转化
                }
        }
        getchar();//注意这个很重要,我检查了半天才发现,否则第二次输入时会转化成一堆乱码
        //puts(b);
        a[0]='$';
        for(i=0;ii? min(p[2*id-i],mx-i):1;
            while(a[i+p[i]]==a[i-p[i]]) p[i]++;
            if(i+p[i]>mx){
                mx=i+p[i];
                id=i;
            }
            if(p[i]>max){max=p[i];t=i;}
        }
        if(max>2){
            if(t%2==1){//分为两种情况对待,优势p[t]最大的时候刚好指着#号
                printf("%d %d\n",(t-1)/2-(max-1)/2,(t-1)/2+(max-1)/2-1);
                for(i=(t-1)/2-(max-1)/2;i<=(t-1)/2+(max-1)/2-1;i++) printf("%c",b[i]);
                printf("\n");
            }
            else{
                printf("%d %d\n",t/2-(max-1)/2-1,t/2+(max-1)/2-1);
                for(i=t/2-(max-1)/2-1;i<=t/2+(max-1)/2-1;i++) printf("%c",b[i]);
                printf("\n");
            }
        }
        else printf("No solution!\n");
        memset(b,'\0',sizeof(b));
        memset(a,'\0',sizeof(a));
        memset(p,'\0',sizeof(p));
        n='\0';
    }
    return 0;
}




你可能感兴趣的:(manacher算法,c语言)