【hihocoder 1032】最长回文子串

【题目链接】:http://hihocoder.com/problemset/problem/1032

【题意】

【题解】

原文地址:https://segmentfault.com/a/1190000003914228
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述


UPD1
RL[j]的定义是,回文最左或最右端到中心点j的距离

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e6+100;

int t,rad[N];
char str[N],s[N];

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
    cin >> t;
    while (t--)
    {
        cin >> str;
        int n = 1;
        s[0] = '$',s[1] = '#';
        for (int i = 0;str[i];i++)
        {
            s[++n] = str[i];
            s[++n] = '#';
        }
        int maxright = 0,pos=0;
        rep1(i,1,n)
        {
            if (maxright>i)
                rad[i] = min(maxright-i,rad[pos*2-i]);
            else
                rad[i] = 1;
            while (s[i-rad[i]]==s[i+rad[i]]) rad[i]++;
            if (i+rad[i]-1>maxright)
            {
                maxright = i+rad[i]-1;
                pos = i;
            }
        }
        int ans = rad[1]-1;
        rep1(i,2,n)
            ans = max(ans,rad[i]-1);
        cout << ans << endl;
    }
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(84)  评论(0编辑  收藏  举报