hdu-3249-Girls' research -mancher

题目传送门:https://vjudge.net/problem/HDU-3294



就是manacher求一下回文串,同时记录一下起始位置和长度,输出。先进行字母的替换。



感觉,最不好推得是,根据变形后的字符串去找现在字符串的位置。


 l = (i - 1)/2 - (ans - 1)/2;
            r = l + ans - 2;


ac代码:

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define N 200005
#define ll long long
using namespace std;

char a[N];
char s[2*N];
int p[2*N], len, l, r;

int manacher()
{
    int pos = 0, maxr = 0, ans = 0;
    for(int i = 2; i <= 2*len; i++)
    {
        p[i] = i < maxr ? min(p[2*pos-i], maxr -i) : 1;
        while(s[i+p[i]] == s[i-p[i]])
            p[i]++;
        if(i + p[i] > maxr)
            maxr = i + p[i], pos = i;

        if(ans < p[i])
        {
            ans = p[i];
            l = (i - 1)/2 - (ans - 1)/2;
            r = l + ans - 2;
        }
    }
    return ans - 1;
}
int main()
{
    char ch;
    while(scanf("%c %s%*c", &ch, a+1)!=EOF)
    {
        len = strlen(a+1);
        s[0] = '$';
        s[1] = '#';
        int c = ch;
        for(int i = 1; i <= len; i++)
        {
            s[i*2+1] = '#';
            int b = a[i];
            s[i*2] = abs(b - c + 26) % 26 + 'a';
        }
        int ans = manacher();
        if(ans <= 1)
        {
            printf("No solution!\n");
        }
        else
        {
            printf("%d %d\n", l, r);
            for(int i = l+1; i <= r+1; i++)
            {
                int b = a[i];
                printf("%c", abs(b - c + 26) % 26 + 'a');
            }
            printf("\n");
        }

    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/newproblems/article/details/77598666
今日推荐