码迷,mamicode.com
首页 > 其他好文 > 详细

URAL1297 Palindrome

时间:2019-07-31 18:29:00      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:printf   print   amp   log   math   cst   ali   algorithm   std   

1297. Palindrome

题目大意

求一个字符串中的最长回文子串

题解

可用后缀数组在mlogn + nlogn时间内解决
回文串的主要思想之一是枚举对称中心,为了同意处理奇偶长度的回文串,用$将每个字符分隔开(不要忘记首尾也要加,因此WA了很多发)
而后,把这个回文串反转后接在原串后面。枚举对称中心,找到它在反转串的相应位置,两个后缀的LCP即为回文串。
字符串写得少,很多细节要好好考虑很久

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <cmath>

void swap(int &a, int &b){int tmp = a;a = b, b = tmp;}
void swap(int* &a, int* &b){int* tmp = a;a = b;b = tmp;}
int min(int a, int b){return a < b ? a : b;}
int max(int a, int b){return a > b ? a : b;}
void read(int &x)
{
    x = 0;char ch = getchar(), c = ch;
    while(ch < '0' || ch > '9') c = ch, ch = getchar();
    while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
    if(c == '-') x = -x;
}

const int INF = 0x3f3f3f3f;
const int MAXN = 200000 + 10;
const int MAXNUM = 30000 + 10;


struct SuffixArray
{
    int s[MAXN], sa[MAXN], rank[MAXN], height[MAXN];
    int t[MAXNUM], t2[MAXNUM], c[MAXNUM];
    int n;
    void clear()
    {
        n = 0;
        memset(sa, 0, sizeof(sa));
    }
    
    void build_sa(int m)
    {
        ++ n;
        int i,*x = t,*y = t2;
        for(i = 0;i < m;++ i) c[i] = 0;
        for(i = 0;i < n;++ i) x[i] = s[i];
        for(i = 0;i < n;++ i) ++ c[x[i]];
        for(i = 1;i < m;++ i) c[i] += c[i-1];
        for(i = n - 1;i >= 0;-- i) sa[-- c[x[i]]] = i;
        for(int k = 1;k <= n;k <<= 1)
        {
            int p = 0;
            for(i = n - k;i < n;++ i) y[p ++] = i;
            for(i = 0;i < n;++ i) if(sa[i] >= k) y[p ++] = sa[i] - k;
            for(i = 0;i < m;++ i) c[i] = 0;
            for(i = 0;i < n;++ i) c[x[i]] ++;
            for(i = 1;i < m;++ i) c[i] += c[i - 1];
            for(i = n - 1;i>=0;--i) sa[-- c[x[y[i]]]] = y[i];
            swap(x,y);
            p = 1;x[sa[0]] = 0;
            for(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;
        }
        -- n;
    }
    
    void build_height()
    {
        int i, j, k = 0;
        for(i = 1;i <= n;++ i) rank[sa[i]] = i;
        for(i = 0;i < n;++ i)
        {
            if(k) k --;
            j = sa[rank[i]-1];
            while(s[i + k] == s[j + k]) k ++;
            height[rank[i]] = k;
        }
    }
}A;

char s[MAXN];
int mi[40][MAXN], M, lg2[MAXN], pow2[30];

void init()
{
    memset(mi, 0x3f, sizeof(mi));
    for(M = 0;(1 << M) <= A.n;++ M);-- M;
    pow2[0] = 1;
    for(int i = 1;i <= 26;++ i) pow2[i] = (pow2[i - 1] << 1);
    lg2[1] = 0;
    for(int i = 2;i <= 100000;++ i) lg2[i] = lg2[i >> 1] + 1;
    for(int i = 1;i <= A.n;++ i) mi[0][i] = A.height[i];
    for(int i = 1;i <= M;++ i)
        for(int j = 1;j <= A.n;++ j)
            mi[i][j] = min(mi[i - 1][j], mi[i - 1][j + pow2[i - 1]]);
}

int getmi(int l, int r)
{
    return min(mi[lg2[r - l + 1]][l], mi[lg2[r - l + 1]][r - pow2[lg2[r - l + 1]] + 1]);
}

int ans, pos;

int main()
{   
    scanf("%s", s);
    A.n = strlen(s);
    for(int i = A.n - 1, j = (A.n << 1) - 1;i >= 0;-- i, -- j)
        s[j] = s[i], s[-- j] = '$';
    s[A.n << 1] = '$';
    A.n = (A.n << 1) | 1;
    for(int i = 0;i < A.n;++ i) A.s[i] = A.s[A.n + A.n - i] = s[A.n + A.n - i] = s[i];
    A.s[A.n] = 450;
    A.n = (A.n << 1) | 1;
    A.build_sa(500);
    A.build_height();
    
    init();
    
    //调试信息
/*  for(int i = 1;i <= A.n;++ i)
        printf("sa[%d]:%s\n", i, s + A.sa[i]);
    for(int i = 1;i <= A.n;++ i)
        printf("height[%d]:%d\n", i, A.height[i]);*/
    
    for(int i = 0;i < (A.n >> 1);++ i)
    { 
        int l = A.rank[i], r = A.rank[A.n - i - 1];
        if(l > r) swap(l, r);
        int tmp = getmi(l + 1, r);
        if(tmp > ans) ans = tmp, pos = i;
    }
    
    for(int i = pos + ans - 1;i >= pos;-- i)
        if(A.s[i] != '$')
            printf("%c", A.s[i]);
    for(int i = pos + 1;i - pos + 1 <= ans;++ i)
        if(A.s[i] != '$') 
            printf("%c", A.s[i]);
    return 0;
} 

URAL1297 Palindrome

标签:printf   print   amp   log   math   cst   ali   algorithm   std   

原文地址:https://www.cnblogs.com/huibixiaoxing/p/11277896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!