台州学院we are without brain 训练 后缀数组

这些题目我并不一定全是用SA做的,但是还是要标记一下的

K - Extend to Palindrome

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的最长匹配,这个想法还是很明显的

马拉车AC代码

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int len,llen,p[N+N],pp[N],ans;
char s[N],ss[N+N];
void manacher()
{
    int id=0,mx=0;
    for(int i=2; i<=llen; i++)
    {
        if(mx>i)
            p[i]=min(p[2*id-i],mx-i);
        else
            p[i]=1;
        while(ss[i+p[i]]==ss[i-p[i]]) p[i]++;
        if(p[i]+i>mx)mx=p[i]+i,id=i;
        ans=max(ans,p[i]);
    }
}
int main()
{
    while(~scanf("%s",s))
    {
        memset(p,0,sizeof(p));
        len=strlen(s),llen=2*len+1;
        ans=0;
        for(int i=0; i<=len; i++)
            ss[2*i+2]=s[i],ss[2*i+1]='#';
        ss[0]='&';
        manacher();
        for(int i=1; i<=llen; i++)
        {
            if(i+p[i]-1==llen)
            {
                for(int k=1; k<=llen; k++)
                    if(ss[k]!='#')printf("%c",ss[k]);
                for(int k=i-p[i]+1; k>=1; k--)
                    if(ss[k]!='#')printf("%c",ss[k]);
                putchar(10);
                break;
            }
        }
    }
    return 0;
}

KMP的AC代码

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
char s[N],t[N];
int nxt[N];
void pre(char *t)
{
    int i=0,j=-1;
    nxt[0]=-1;
    while(t[i])
    {
        if(j==-1||t[i]==t[j])
        {
            i++,j++;
            if(t[i]!=t[j])nxt[i]=j;
            else nxt[i]=nxt[j];
        }
        else j=nxt[j];
    }
}
int KMP(char *s,char *t)
{
    pre(t);
    int i=0,j=0;
    while(s[i])
    {
        if(j==-1||s[i]==t[j])i++,j++;
        else j=nxt[j];
    }
    return j;
}
int main()
{
    while(~scanf("%s",s))
    {
        int l=strlen(s);
        for(int i=0; i<l; i++) t[i]=s[l-1-i];t[l]=0;
        printf("%s%s\n",s,&t[KMP(s,t)]);
    }
    return 0;
}

SA的做法会比较麻烦的吧

SA找两个串的最长公共子串限定这个子串的范围

猜你喜欢

转载自www.cnblogs.com/BobHuang/p/9137927.html