poj2752Seek the Name, Seek the Fame【kmp next数组应用】

大意:给你一个串,如果这个串存在一个长度为n的前缀串,和长度为n的后缀串,并且这两个串相等,则输出他们的长度n。求出所有的长度n

例如

‘alala‘. Potential prefix-suffix strings of S are {‘a‘, ‘ala‘, ‘alala‘}

分析:考察对于next数组的理解

next数组表示i之前的k个字符与该串钱k个字符匹配

所以

next[l] 就表示最大后缀满足与前缀相同的最大字串

然后  在对获得的字串进行同样的求解  最终得到结果

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int maxn = 400005;
 7
 8 int next[maxn];
 9 int ans[maxn];
10
11 void get(char *s) {
12     int l = strlen(s);
13     int j = 0, k = -1;
14     next[0] = -1;
15     while(j < l) {
16         if(k == -1 || s[j] == s[k]) {
17             next[++j] = ++k;
18         } else {
19             k = next[k];
20         }
21     }
22 }
23 char s[maxn];
24
25 int main() {
26     while(EOF != scanf("%s",s)) {
27         get(s);
28         int tot = 0;
29         int k = strlen(s);
30         while(k) {
31             ans[tot++] = k;
32             k = next[k];
33         }
34         for(int i = tot - 1; i >= 0; i--) {
35             printf(i == tot - 1 ? "%d" : " %d", ans[i]);
36         } puts("");
37     }
38 }

时间: 2024-08-24 16:14:49

poj2752Seek the Name, Seek the Fame【kmp next数组应用】的相关文章

poj 2752 Seek the Name, Seek the Fame KMP

对于KMP算法中next函数的应用 题意是对于一个字符串的前缀和后缀比较是否相等,再把相等的可能按字符串长度进行输出 #include <iostream> #include<stdio.h> #include<string.h> using namespace std; int len; int next[1000005]; char s[1000005]; int kmp_next() { int i=0,j=-1; next[0]=-1; while(i<l

POJ 2752 Seek the Name, Seek the Fame KMP题解

本题是KMP的next数组的灵活运用. 具体就是看最后整个数列的最后一个字母,能有多少前缀. 理解了next数组就很容易了. #include <stdio.h> #include <string.h> #include <vector> using std::vector; const int MAX_N = 400001; char name[MAX_N]; int next[MAX_N], len; void genNext() { for (int i = 1,

poj2752 Seek the Name, Seek the Fame(next数组的运用)

题目链接:http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12018   Accepted: 5906 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and

Poj2752--Seek the Name, Seek the Fame(Kmp → → Next数组应用)

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14172   Accepted: 7055 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names t

POJ 2752 Seek the Name, Seek the Fame kmp失配函数next应用

点击打开链接 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12791   Accepted: 6304 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give

poj2752Seek the Name, Seek the Fame

Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 11   Accepted Submission(s) : 6 Problem Description The little cat is so famous, that many couples tramp over hill an

poj——2752Seek the Name, Seek the Fame(kmp专练 找出前后相同的字串)

Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16035 Accepted: 8158 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to th

POJ 2752 Seek the Name, Seek the Fame (KMP的next函数运用)

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14188   Accepted: 7068 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names t

POJ - 2752 Seek the Name, Seek the Fame (KMP的next[]应用)

Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such b

POJ2752 Seek the Name, Seek the Fame (kmp) 题解

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17094   Accepted: 8720 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names t