Uva 11475 : Extend to Palindrome(Hash或KMP)

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


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


大神们也不教我……


好可怜好可怜……


然后就用的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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值