UVA-11475-Extend to Palindrome((擴展)kmp)

UVA-11475-Extend to Palindrome((擴展)kmp)

題目:
Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse
it) of length at least N. Any palindrome will do. Easy, isn’t it? That’s what you thought before you
passed it on to your inexperienced team-mate. When the contest is almost over, you find out that
that problem still isn’t solved. The problem with the code is that the strings generated are often not
palindromic. There’s not enough time to start again from scratch or to debug his messy code. Seeing
that the situation is desperate, you decide to simply write some additional code that takes the output
and adds just enough extra characters to it to make it a palindrome and hope for the best. Your
solution should take as its input a string and produce the smallest palindrome that can be formed by
adding zero or more characters at its end.
Input
Input will consist of several lines ending in EOF. Each line will contain a non-empty string made up of
upper case and lower case English letters (‘A’-‘Z’ and ‘a’-‘z’). The length of the string will be less than
or equal to 100,000.
Output
For each line of input, output will consist of exactly one line. It should contain the palindrome formed
by adding the fewest number of extra letters to the end of the corresponding input string.
Sample Input
aaaa
abba
amanaplanacanal
xyz

Sample Output
aaaa
abba
amanaplanacanalpanama
xyzyx

題意:給定一個字符串,在後面添加最少的字符使其變成迴文串。

很多種做法,kmp,擴展kmp,後綴數組都可以,這裏給出擴展kmp的做法
原串作S串,翻轉串作T串進行擴展kmp,遍歷一次extand數組,當extand[i]=i時,迴文部分最大,然後輸出即可。

AC代碼:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <cmath>
#include <vector>
using namespace std;
int    nxt[100005];
int extand[100005];
char     S[100005];
char     T[100005];
void getnext()
{
    int s=strlen(T);
    nxt[0]=s;
    int t=0;
    for(t=0;t<s-1&&T[t]==T[t+1];t++);
    nxt[1]=t;
    int a=1;
    for(int k=2;k<s;k++){
        int p=a+nxt[a]-1,len=nxt[k-a];
        if(k-1+len>=p){
            int j= (p-k+1)>0 ? (p-k+1) : 0;
            while(j+k<s&&T[j+k]==T[j]) j++;
            nxt[k]=j; a=k;
        }
        else nxt[k]=len;
    }
}
void getextand()
{
    getnext();
    int s=strlen(S),t=strlen(T);
    int minlen=min(s,t);
    int a=0;
    while(a<minlen&&S[a]==T[a]) a++;
    extand[0]=a;
    a=0;
    for(int k=1;k<s;k++){
        int p=a+extand[a]-1,len=nxt[k-a];
        if(k-1+len>=p){
            int j=max(p-k+1,0);
            while(j+k<s&&j<t&&S[j+k]==T[j]) j++;
            extand[k]=j;a=k;
        }
        else extand[k]=len;
    }
}
int main()
{
    //freopen("out.txt","w",stdout);
    while(scanf("%s",S)!=EOF){
        int n=strlen(S);
        for(int i=0;i<n;i++){
            T[i]=S[n-1-i];
        }
        T[n]=0;
        //printf("%s %s\n",S,T);
        getextand();
        int maxn=0;
        for(int i=0;i<n;i++){
            if(extand[i]==n-i){
                maxn=extand[i];
                break;
            }
        }
        printf("%s%s\n",S,T+maxn);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章