您的位置:首页 > 其它

POJ 2752 Seek the Name, Seek the Fame

2015-08-17 22:04 441 查看
题目链接:

http://poj.org/problem?id=2752

解题思路:

给你一个字符串s,从小到大输出s中既是前缀又是后缀的子串的长度。

利用next数组的存储的性质,即可得出正确的答案。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;

const int N = 400005;
int Next
,ans
;
char s
;

void getnext(){
    int j = 0,k = -1;
    int l = strlen(s);
    Next[0] = -1;
    while(j < l){
        if(k == -1 || s[j] == s[k]){
            j++;k++;
            Next[j] = k;
        }
        else
            k = Next[k];
    }
}

void kmp(){
    getnext();
    int l = strlen(s);
    int i = l,sum = 0;
    while(i != 0){
        ans[sum++] = Next[i];
        i = Next[i];
    }
    for(i = sum-2; i >= 0; i--)
        printf("%d ",ans[i]);
    printf("%d\n",l);
}

int main(){
    while(~scanf("%s",s)){
        kmp();
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: