POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)

题目链接:http://poj.org/problem?id=2752

题意:给你一个字符串,求出所有前缀后缀(既是前缀又是后缀的子串)的长度

思路:首先整个字符串肯定既是前缀又是后缀,为最大的前缀后缀。

假设next[len] = k,也即:s[1,k] = s[len-k+1,len]此时s[1,k]是前缀后缀。

处理完next[len]后跳转到next[k+1],用这种方法可以得到所有的前缀后缀。

code:

 1 #include <cstdio>
 2 #include <cstring>
 3 const int MAXN = 400005;
 4 char str[MAXN];
 5 int next[MAXN];
 6 int ans[MAXN];
 7 void GetNext(int len)
 8 {
 9     int i = 0;
10     int j = -1;
11     next[0] = -1;
12     while (i < len)
13     {
14         if (-1 == j || str[i] == str[j]) next[++i] = ++j;
15         else j = next[j];
16     }
17 }
18
19 int main()
20 {
21     while (scanf("%s", str) == 1)
22     {
23         int len = strlen(str);
24         GetNext(len);
25         int t = -1;
26         ans[++t] = len;
27         while (next[len] > 0)
28         {
29             ans[++t] = next[len];
30             len = next[len];
31         }
32         for (int i = t; i > 0; --i) printf("%d ", ans[i]);
33         printf("%d\n", ans[0]);
34     }
35     return 0;
36 }
时间: 2024-08-27 21:41:51

POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)的相关文章

POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)

题目链接:http://poj.org/problem?id=2752 题目大意:给你一串字符串s找到所有的公共前后缀,即既是前缀又是后缀的子串. 解题思路: 如图所示 假设字符串pi与jq为符合条件的一组公共前后缀,那么易得pi=jq,如下图所示 若在字符串pi内,pk1与k2i为公共前后缀,有因为pi=jq所以对应的k2i在字符串jq内有后缀k4q与其等价.所以pi内的公共前后缀等也是pq的公共前后缀,而i=next[q],显然是个递归. 所以,可以通过不断使pos=next[pos]进行递

poj_2752 Seek the Name, Seek the Fame

http://poj.org/problem?id=2752 分析: 题目要求一个既是S的前缀又是S的后缀的子串(S本身是自己的前缀又是自己的后缀).主要利用next数组求解.       e.g. no 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 S a b a b c a b a b a b a b c a b a b /0 next -1 0 0 1 2 0 1 2 3 4 3 4 3 4 5 6 7 8 9 Out: 2  4  9  1

poj2752 Seek the Name, Seek the Fame

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17348   Accepted: 8879 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

对于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

(KMP)Seek the Name, Seek the Fame -- poj --2752

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

Seek the Name, Seek the Fame poj 2752

Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Problem Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give n

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,

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher H - Seek the Name, Seek the Fame POJ - 2752(kmp的next数组应用)

H - Seek the Name, Seek the Fame POJ - 2752 题目链接:https://vjudge.net/contest/70325#problem/H 题目: 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. Th

KMP中next的应用 POJ 2752 Seek the Name, Seek the Fame

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19163   Accepted: 9849 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