【Manacher Algorithm】HDU 3294 Girls' research

HDU 3294 Girls’ research

题意:

  • 给一个只有小写字母的字符串,然后重新定义 ’ a ’ ,剩下的依次后延。比如:如果定义b is real ‘a’,那么c is real ‘b’,d is real ‘c’,…,a is real ‘z’,依次类推。
  • 然后从真正的字符串中找最大回文串,如果回文串的长度大于等于2,输出回文串的起始位置,然后输出该回文串,如果有多个答案,输出第一个。否则,没有答案。

思路:

AC_Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxN = 11e4 + 5;

int len;
struct node{
    string str;
    int start, length;
};
node Manacher(string &s)
{
    string t = "$#";
    for(int i = 0; i < len; i ++ )
    {
        t += s[i];
        t += "#";
    }
    len = t.length();
    vector<int>radius(len, 0);
    int mx = 0, id = 0, Center = 0, Len = 0;
    for(int i = 1; i < len ; i ++ )
    {
        radius[i] = mx > i ? min(mx - i , radius[(id << 1) - i]) : 1;
        while(i + radius[i] < len && i - radius[i] >= 0 && t[i + radius[i]] == t[i - radius[i]])
            ++ radius[i];
        if(mx < i + radius[i])
        {
            mx = i + radius[i];
            id  = i;
        }
        if(Len < radius[i])
        {
            Len = radius[i];
            Center = i;
        }
    }
    return node{s.substr((Center - Len) >> 1, Len - 1), (Center - Len) >> 1, Len - 1};
}

int main()
{
    char ch[5];
    string s;
    while(~scanf("%s", ch))
    {
        cin >> s;
        len = s.length();
        for(int i = 0; i < len; i ++ )
        {
            if(s[i] >= ch[0])
                s[i] = s[i] + ( 'a' - ch[0] );
            else
                s[i] = 'z' + (s[i] - ch[0]) + 1;
        }
        node ans = Manacher(s);
        if(ans.length < 2)
            printf("No solution!\n");
        else
        {
            printf("%d %d\n", ans.start, ans.start + ans.length - 1);
            cout << ans.str << endl;
        }
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值