URAL 1297(最长回文串)

题目:http://acm.timus.ru/problem.aspx?space=1&num=1297

题目大意:给你一个字符串,问你输出其中最长的回文串,如果有多个,就输出最早出现的那个。

思路:把那个字符串倒过来然后黏在后面,中间用一个不同的ASC码,然后做后缀数组,然后枚举中间位置i,对于每个i,要考虑回文串是奇数和偶数的情况,然后求 lcp,注意空间开两倍,RE了一次,挺基础的一个题目,然后就木有然后了。。= =

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN = 1111<<1;

char str[MAXN];

int sa[MAXN],rank[MAXN],height[MAXN];

int t[MAXN],t2[MAXN],c[MAXN];

void cal_height(int n)
{
    for(int i = 1;i <= n;i++) rank[sa[i]] = i;
    int k = 0;
    for(int i = 0;i < n;i++)
    {
        if(k) k--;
        int j = sa[rank[i]-1];
        while(str[i+k] == str[j+k]) k++;
        height[rank[i]] = k;
    }
}

void da(int n,int m)
{
    int *x = t,*y = t2;
    for(int i = 0;i < m;i++) c[i] = 0;
    for(int i = 0;i < n;i++) c[x[i] = str[i]]++;
    for(int i = 1;i < m;i++) c[i] += c[i-1];
    for(int i = n-1;i >= 0;i--) sa[--c[x[i]]] = i;
    for(int k = 1;k <= n;k <<= 1)
    {
        int p = 0;
        for(int i = n-k;i < n;i++) y[p++] = i;
        for(int i = 0;i < n;i++) if(sa[i] >= k) y[p++] = sa[i] - k;
        for(int i = 0;i < m;i++) c[i] = 0;
        for(int i = 0;i < n;i++) c[x[y[i]]]++;
        for(int i = 1;i < m;i++) c[i] += c[i-1];
        for(int i = n-1;i >=0;i--) sa[--c[x[y[i]]]] = y[i];
        swap(x,y);
        p = 1;
        x[sa[0]] = 0;
        for(int i = 1;i < n;i++)
            x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k] ? p-1 : p++;
        if(p >= n) break;
        m = p;
    }
    cal_height(n-1);
}

int d[MAXN][20];

void rmq_init(int n)
{
    for(int i = 1;i <= n;i++)
        d[i][0] = height[i];
    for(int j = 1;(1<<j) <= n;j++)
        for(int i = 1;i+(1<<j) <= n;i++)
            d[i][j] = min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}

int rmq(int a,int b)
{
    int k = 0;
    while((1<<(k+1)) < (b-a+1)) k++;
    return min(d[a][k],d[b-(1<<k)+1][k]);
}

int lcp(int a,int b)
{
    a = rank[a],b =rank[b];
    if(a > b) swap(a,b);
    return rmq(a+1,b);
}

int main()
{
    while(~scanf("%s",str))
    {
        int n = strlen(str);
        str[n] = 127;
        for(int i = 0;i < n;i++)
            str[n+1+i] = str[n-i-1];
        int nn = (n<<1)+1;
        str[nn] = 0;
        //for(int i = 0;i<nn;i++)
            //printf("%d\n",str[i]);
        da(nn+1,256);
        int kind = -1;
        int ans_len = 0,pos;
        rmq_init(nn);
        for(int i = 0;i < n;i++)
        {
            int tmp;
            if(i > 0)
            {
                tmp = lcp(i,2*n-(i-1))*2;
                if(tmp > ans_len)
                {
                    ans_len = tmp;
                    pos = i;
                    kind = 0;
                }
                //printf("i = %d,tmp1 = %d\n",i,tmp);
            }
            tmp = 2*lcp(i+1,2*n-(i-1))+1;
            if(tmp > ans_len)
            {
                ans_len = tmp;
                pos = i;
                kind = 1;
            }
            //printf("i = %d,tmp2 = %d\n",i,tmp);
        }
        //printf("ans_len = %d,pos = %d\n",ans_len,pos);
        if(kind == 0)
        {
            int tt = ans_len/2;
            for(int i = pos-tt;i <= pos+tt-1;i++)
                printf("%c",str[i]);
        }
        else if(kind == 1)
        {
            int tt = (ans_len-1)/2;
            for(int i = pos-tt;i <= pos+tt;i++)
                printf("%c",str[i]);
        }
        puts("");
    }
    return 0;
}


你可能感兴趣的:(URAL 1297(最长回文串))