HDU 3294Girls' research最长回文子串(暴力和manacher 两种解法)

Girls' research

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 444    Accepted Submission(s): 148


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,比如是c,然后d就代表b依次类推。求出后面的串的最长回文子串。如果长度小于2输出No solution!这个题目与上一个best reward有点不同,那个题目是从中间切断判断两半是否回文,所以可以用EKMP。但是这题是让你求任意的子串。

            解题思路:先给一个暴力解法,很容易理解。从任意点可以往外扩展的最长的距离。不过需要分奇偶。

            题目地址:Girls' research

暴力AC代码:
#include
#include
#include
#include
using namespace std;
char str[200005];

int main()
{
    char tmp[5];
    int i,p,len,j;
    while(~scanf("%s %s",&tmp,str))
    {
        p=tmp[0]-'a';
        len=strlen(str);
        for(i=0;i=0,i+j+1res)
                {
                    res=2*j+2;
                    left=i-j;
                    right=i+j+1;
                }
            }
            for(j=0;i-j>=0,i+jres)
                {
                    res=2*j+1;
                    left=i-j;
                    right=i+j;
                }
            }
        }

        if(res<2)
                puts("No solution!");
            else
            {
                printf("%d %d\n",left,right);
                for(i=left;i<=right;i++)
                    printf("%c",str[i]);
                puts("");
            }

    }
    return 0;
}
//140MS 356K


           后来上网看了下,有个manacher算法是专门解决最长回文子串的问题。它的解法避开了奇偶的讨论,因为它在每个字符中间都插入了#然后第一个插入*。

见AC代码:
#include
#include
#include
#include
using namespace std;
char str[200005],s[400005];
int len,p[4000005];

void manacher()
{
    int i;
    s[0]='*',s[1]='#';
    for(i=0;ii)p[i]=min(p[2*id-i],mx-i);
        else p[i]=1;
        for(;s[i-p[i]]==s[i+p[i]];p[i]++);
        if(p[i]+i>mx)mx=p[i]+i,id=i;
        if(p[i]-1>res)
        {
              res=p[i]-1;
              left=(i-p[i]+2)/2-1;
              right=left+p[i]-2;
        }
    }

    if(res<2)
        puts("No solution!");
    else
    {
        printf("%d %d\n",left,right);
        for(i=left;i<=right;i++)
            printf("%c",str[i]);
        puts("");
    }

}

int main()
{
    char tmp[5];
    int i,p,j;
    while(~scanf("%s %s",&tmp,str))
    {
        p=tmp[0]-'a';
        len=strlen(str);
        for(i=0;i


你可能感兴趣的:(字符串)