HDU 3294 Girls' research

Girls’ research

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

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是哪一个。

思路:

就是马拉车算法裸题,用马拉车算法求出最长回文的Mp数组,然后根据数组确定最长回文的中间的id是多少,然后再算出起始和结束的位置多少。

#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 2e+6 + 10;
char Ma[maxn * 2], s[maxn];
int Mp[maxn * 2];
void Manacher(char *s, int len) {
    int l = 0;
    Ma[l++] = '$';
    Ma[l++] = '#';
    for (int i = 0; i < len; i++) {
        Ma[l++] = s[i];
        Ma[l++] = '#';
    }
    int mx = 0, id = 0;
    for (int i = 0; i < l; i++) {
        if (mx > i) Mp[i] = min(mx - i, Mp[2 * id - i]);
        else Mp[i] = 1;
        while (Ma[i - Mp[i]] == Ma[i + Mp[i]]) Mp[i]++;
        if (i + Mp[i] > mx) {
            mx = i + Mp[i];
            id = i;
        }
    }
}
int main() {
    char c;
    while (scanf("%c %s", &c, s) != EOF) {
        getchar();
        int len = strlen(s);
        Manacher(s, len);
        int ans = 0, k = 0;
        for (int i = 0; i < 2 * len + 2; i++) {
            if (ans < Mp[i] - 1) {
                ans = Mp[i] - 1;
                k = (i - 1) / 2;
            }
        }
        if (ans < 2) {
            printf("No solution!\n");
            continue;
        }
        k -= ans / 2;
        printf("%d %d\n", k, k + ans - 1);
        for (int i = k; i <= k + ans - 1; i++) {
            printf("%c", (s[i] - c + 26) % 26 + 'a');
        }
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(HDU)