Girls' research HDU - 3294 (Manacher算法)

本文介绍了一种特殊的字符串操作挑战,即寻找经过特定转换后的最长回文子串。通过对输入字符串进行预处理和使用Manacher算法,文章详细解释了如何高效地找到满足条件的回文串,并给出了完整的C++代码实现。

Girls' research

HDU - 3294

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!

加个记录原来字符串位置的记录数组就可以了,我的做法很笨

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 200100;
char op;
char s[MAXN];
char News[MAXN<<1];
int len[MAXN<<1];
int index[MAXN<<1];
int init(int n){
    int i,cnt = 0,j = 0,p = 0;
    News[cnt++] = '@';
    index[j++] = -1;
    News[cnt++] = '#';
    index[j++] = -1;
    for(i = 0; i < n; i++){
        News[cnt++] = s[i];
        index[j++] = p++;
        News[cnt++] = '#';
        index[j++] = -1;
    }
    News[cnt++] = '$';
    index[j++] = -1;
    News[cnt] = '\0';
    return cnt-1;
}
int Manacher(int n){
    int i;
    int mx = 0,po = 0,ans = 0,ansp;
    for(i = 0; i < n; i++){
        if(mx > i)
            len[i] = min(mx-i,len[2*po-i]);
        else
            len[i] = 1;
        while(News[i-len[i]] == News[i+len[i]])
            len[i]++;
        if(i+len[i] > mx){
            mx = i+len[i];
            po = i;
        }
    }
}
int main(){
    while(~scanf("%c",&op)){
        scanf("%s",s);
        getchar();
        int i,ansp,ans = 0;
        int n = strlen(s);
        for(i = 0; i < n; i++){
            if(s[i]-op < 0)
                s[i] = 26+(s[i]-op)+'a';
            else
                s[i] = s[i]-op+'a';
        }
        n = init(n);
        Manacher(n);
        for(i = 1; i < n; i++){
            if(len[i]-1 > ans){
                ans = len[i]-1;
                ansp = i;
            }
        }
        if(ans < 2)
            printf("No solution!\n");
        else{
            int st = ansp-ans,ed = ansp+ans;
            int ss,ee;
            for(i = st; i <= ed; i++){
                if(News[i] != '#'){
                    ss = index[i];
                    break;
                }
            }
            for(i = ed; i >= st; i--){
                if(News[i] != '#'){
                    ee = index[i];
                    break;
                }
            }
            printf("%d %d\n",ss,ee);
            for(i = ss; i <= ee; i++)
                printf("%c",s[i]);
            printf("\n");
        }

    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值