现在的位置: 首页 > 综合 > 正文

Uva 11475 : Extend to Palindrome(Hash或KMP)

2018年01月20日 ⁄ 综合 ⁄ 共 676字 ⁄ 字号 评论关闭

不会做不会做不会做不会做不会做……委屈

呜呜呜呜呜呜呜呜呜呜呜呜……

大神们也不教我……

好可怜好可怜……

然后就用的KMP做的……用next数组记录下特殊情况,当非回文的时候需要跳转的位置进行重新判断……【其实我真的不懂啊55555555555……求大神讲解

就假装是这样吧。。。

AC  Memory : 0KB    TIme : 59MS

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
const int maxn=100100;
int next[maxn];
string a,rev;
void getnext()
{
    int len=rev.size();
    int i=0,j=-1;
    next[0]=-1;
    while(i<len)
    {
        if(rev[i]==rev[j]||j==-1)
        {
            i++,j++;
            next[i]=j;
        }
        else j=next[j];
    }
}

int main()
{
    while(cin>>a)
    {
        rev=a;
        reverse(rev.begin(),rev.end());
        getnext();

        int ans=0;
        int cur=0,p=0,len=a.size();
        while(cur<len)
        {
            if(rev[p]==a[cur])
            {
                p++,cur++;
                ans=p;
            }
            else if(p>=0)p=next[p];
            else cur++,p=0;
        }
        cout<<a<<rev.substr(ans)<<endl;
    }
    return 0;
}

抱歉!评论已关闭.