KMP POJ 2752 Seek the Name, Seek the Fame

时间:2022-04-23 07:40:15

题目传送门

 /*
题意:求出一个串的前缀与后缀相同的字串的长度
KMP:nex[]就有这样的性质,倒过来输出就行了
*/
/************************************************
* Author :Running_Time
* Created Time :2015-8-10 11:29:25
* File Name :POJ_2752.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 4e5 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
int nex[MAXN];
char str[MAXN]; void get_nex(void) {
int len = strlen (str);
int i = , j = -; nex[] = -;
while (i < len) {
if (j == - || str[j] == str[i]) {
j++; i++; nex[i] = j;
}
else j = nex[j];
}
vector<int> ans;
while (i != -) {
ans.push_back (i); i = nex[i];
}
for (int i=ans.size ()-; i>=; --i) {
printf ("%d ", ans[i]);
}
puts ("");
} int main(void) { //POJ 2752 Seek the Name, Seek the Fame
while (scanf ("%s", str) == ) {
get_nex ();
} return ;
}