[后缀数组]ural 1354:Palindrome. Again Palindrome

大致题意:
    给出一个字符串,要求在这个字符串后面添加最少的字母使其成为回文串。

 

大致思路:
    要注意添加的字母数不能为0,要分奇偶讨论。

 

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int nMax =1000012;

int  num[nMax];
int sa[nMax], rank1[nMax], height[nMax];
int wa[nMax], wb[nMax], wv[nMax], wd[nMax];

int cmp(int *r, int a, int b, int l){
    return r[a] == r[b] && r[a+l] == r[b+l];
}

void da(int *r, int n, int m){          //  倍增算法 r为待匹配数组  n为总长度 m为字符范围
    int i, j, p, *x = wa, *y = wb, *t;
    for(i = 0; i < m; i ++) wd[i] = 0;
    for(i = 0; i < n; i ++) wd[x[i]=r[i]] ++;
    for(i = 1; i < m; i ++) wd[i] += wd[i-1];
    for(i = n-1; i >= 0; i --) sa[-- wd[x[i]]] = i;
    for(j = 1, p = 1; p < n; j *= 2, m = p){
        for(p = 0, i = n-j; i < n; i ++) y[p ++] = i;
        for(i = 0; i < n; i ++) if(sa[i] >= j) y[p ++] = sa[i] - j;
        for(i = 0; i < n; i ++) wv[i] = x[y[i]];
        for(i = 0; i < m; i ++) wd[i] = 0;
        for(i = 0; i < n; i ++) wd[wv[i]] ++;
        for(i = 1; i < m; i ++) wd[i] += wd[i-1];
        for(i = n-1; i >= 0; i --) sa[-- wd[wv[i]]] = y[i];
        for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i ++){
            x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p - 1: p ++;
        }
    }
}

void calHeight(int *r, int n){           //  求height数组。
    int i, j, k = 0;
    for(i = 1; i <= n; i ++) rank1[sa[i]] = i;
    for(i = 0; i < n; height[rank1[i ++]] = k){
        for(k ? k -- : 0, j = sa[rank1[i]-1]; r[i+k] == r[j+k]; k ++);
    }
}

int Log[nMax];
int best[20][nMax];
void initRMQ(int n) {//初始化RMQ
    for(int i = 1; i <= n ; i ++) best[0][i] = height[i];
    for(int i = 1; i <= Log[n] ; i ++) {
        int limit = n - (1<<i) + 1;
        for(int j = 1; j <= limit ; j ++) {
            best[i][j] = min(best[i-1][j] , best[i-1][j+(1<<i>>1)]);
        }
    }
}
int lcp(int a,int b) {//询问a,b后缀的最长公共前缀
    a = rank1[a];    b = rank1[b];
    if(a > b) swap(a,b);
    a ++;
    int t = Log[b - a + 1];
    return min(best[t][a] , best[t][b - (1<<t) + 1]);
}

char str[nMax];

int main(){
    int i,j,n,cas=0,len;
    Log[0] = -1;
    for(int i=1;i<=nMax;i++){
        Log[i]=(i&(i-1))?Log[i-1]:Log[i-1] + 1 ;
    }
    while(scanf("%s",str)!=EOF){
        len=strlen(str);
        n=0;
        for(i=0;i<len;i++){
            num[n++]=str[i];
        }
        num[n++]=126;
        for(i=len-1;i>=0;i--){
            num[n++]=str[i];
        }
        num[n]=0;
        memset(height,0,sizeof(height));
        da(num, n + 1,130);
        calHeight(num,n);
        initRMQ(n);
        int start,ans=1,tmp;
        int ans1=len-1,ans2=len-1;
        int len1,len2,fuck=0;
        for(i=len-1;i>0;i--){
            tmp=lcp(i,n-i-1);    ///首先考虑回文长度是奇数的情况
       //     printf("1lcp(%d,%d)=%d\n",i,n-i-1,tmp);
            if(tmp>=len-i){
                if(len%2==1&&i==len/2)continue;
                ans1=i;
            }
            tmp=lcp(i,n-i);    ///考虑回文长度是偶数的情况
          //  printf("lcp(%d,%d)=%d  len-i=%d\n",i,n-i,tmp,len-i);
            if(tmp>=len-i){//&&((!len&1)&&i==len/2)){
                if(len%2==0){
                    if(i==len/2){
                        continue;
                    }
                }
                fuck=1;
                ans2=i;
             //   cout<<"fffuuuccckkk\n";
            }
        }
        if(len==1){
            cout<<str<<str<<endl;
            continue;
        }
     //   cout<<ans1<<" "<<ans2<<" "<<fuck<<endl;
        len1=(ans1)*2+1;
        len2=ans2*2;
        if(len1<len2||!fuck){
            for(i=0;i<ans1;i++)cout<<str[i];
            for(i=ans1;i>=0;i--)cout<<str[i];
            cout<<endl;
        }
        else{
            for(i=0;i<ans2;i++)cout<<str[i];
            for(i=ans2-1;i>=0;i--)cout<<str[i];
            cout<<endl;
        }
    }
    return 0;
}
 

你可能感兴趣的:(数据结构,字符串,ACM,后缀数组,ural 1354)