首页 > 编程语言 > 详细

POJ2752 Seek the Name, Seek the Fame 题解 KMP算法

时间:2019-11-04 21:07:06      阅读:79      评论:0      收藏:0      [点我收藏+]

题目链接:http://poj.org/problem?id=2752
题目大意:给你一个字符串 \(S\) ,如果它的一个前缀同时也是它的后缀,则输出这个前缀(后缀)的长度。
题目分析:next函数的一个应用。我们设 \(S\) 串的长度为 \(n\) ,则我们首先设 \(j=n-1\) ,然后只要 \(nxt[j] != -1\) ,那么?\(j+1\)?就是一个答案,循环使?\(j=nxt[j]\)?。
实现代码如下:

#include <iostream>
#include <string>
using namespace std;
const int maxn = 404000;

int m, nxt[maxn];
string t;
char ch[maxn];

void cal_next() {
    m = t.length();
    for (int i = 0, j = -1; i < m; i ++) {
        while (j != -1 && t[j+1] != t[i]) j = nxt[j];
        nxt[i] = (j+1 < i && t[j+1] == t[i]) ? ++j : -1;
    }
}

bool output(int j) {
    if (j == -1) return false;
    if (output(nxt[j])) cout << " ";
    cout << j + 1;
    return true;
}

int main() {
    while (cin >> t) {
        cal_next();
        output(m-1);
        cout << endl;
    }
    return 0;
}

作者:zifeiy

POJ2752 Seek the Name, Seek the Fame 题解 KMP算法

原文:https://www.cnblogs.com/codedecision/p/11794486.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!