POJ2752 Seek the Name, Seek the Fame 【KMP】

Seek the Name, Seek the Fame

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11602   Accepted: 5680

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 boring job, the innovative
little cat works out an easy but fantastic algorithm:

Step1. Connect the father‘s name and the mother‘s name, to a new string S.

Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).

Example: Father=‘ala‘, Mother=‘la‘, we have S = ‘ala‘+‘la‘ = ‘alala‘. Potential prefix-suffix strings of S are {‘a‘, ‘ala‘, ‘alala‘}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings
of S? (He might thank you by giving your baby a name:)

Input

The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.

Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.

Output

For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby‘s name.

Sample Input

ababcababababcabab
aaaaa

Sample Output

2 4 9 18
1 2 3 4 5

题意:给定一个串,求它的子串的长度,子串满足既是主串的前缀,又是主串的后缀,将所有满足要求的子串长度按照升序输出。

题解:next数组的简单运用,(但是读懂题意用了好久啊。。)求出next数组后递归输出相应的后缀长度,len要单独输出。

#include <stdio.h>
#define maxn 400002

char str[maxn];
int next[maxn], len;

void getNext()
{
	int i = 0, j = -1;
	next[0] = -1;
	while(str[i]){
		if(j == -1 || str[i] == str[j]){
			++i; ++j;
			next[i] = j; //mode 1
		}else j = next[j];
	}
	len = i;
}

void getVal(int n)
{
	if(next[n] == 0) return;
	getVal(next[n]);
	printf("%d ", next[n]);
}

int main()
{
	//freopen("stdin.txt", "r", stdin);
	while(scanf("%s", str) == 1){
		getNext();
		getVal(len);
		printf("%d\n", len);
	}
	return 0;
}
时间: 2024-10-07 00:53:12

POJ2752 Seek the Name, Seek the Fame 【KMP】的相关文章

poj 2752Seek the Name, Seek the Fame 【kmp】

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

POJ2752 Seek the Name, Seek the Fame【KMP】

题目链接: http://poj.org/problem?id=2752 题目大意: 给定一个字符串S,计算出所有可能的前缀-后缀字符串的长度.前缀-后缀字符串指的是S的 子串不仅是S的前缀,还是S的后缀.比如S = "alala",前缀-后缀字符有{"a","ala","alala"}. 思路: KMP算法的应用.在KMP算法中,当字符串匹配失败时,模式串的指针并没有指向0从头比 较,而是指向了一个特定的位置,因为这个Nex

POJ--2752--Seek the Name, Seek the Fame【KMP】

链接:http://poj.org/problem?id=2752 题意:对于一个字符串S,可能存在前n个字符等于后n个字符,从小到大输出这些n值. 思路:这道题加深了对next数组的理解.next[i+1]相当于以第i位结尾的长度为next[i+1]的子串与前next[i+1]个字符组成的子串相同,理解之后就比较好做了,首先字符串的长度len肯定是一个答案,然后next[len]也是一个答案,原因如红字所写,如此迭代直到next下标值等于0停止,这是从大到小得到了答案,再反序输出即可. 因为这

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

POJ3461 Oulipo 【KMP】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22295   Accepted: 8905 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote

【KMP】【最小表示法】NCPC 2014 H clock pictures

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个针都是相同的,分别指向Ai,Bi(360°被分成360000小份),问能否将其中一个旋转和另一个重合. 题目思路: [KMP][最小表示法] 循环同构问题.可以写KMP,我懒得写KMP了就写了循环同构的最小表示法. 首先将Ai排序,然后求差(记得取模360000,WA了一次),接下来复制一遍开始匹配. A

【动态规划】【KMP】HDU 5763 Another Meaning

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成2种意思,问s1可以解读成几种意思(mod 1000000007). 题目思路: [动态规划][KMP] 题目有点绕,看看样例就懂了.其实不用KMP直接用substr就能做. 首先不解读成另一个意思的话,f[i]=f[i-1],接着如果当前位置能够与s2匹配,那么f[i]+=f[i-strlen(s2)]

HDU3746 Cyclic Nacklace 【KMP】

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2538    Accepted Submission(s): 1154 Problem Description CC always becomes very depressed at the end of this month, he has checke

NYOJ327 亲和串 【KMP】

亲和串 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 最近zyc遇到了一个很棘手的问题:判断亲和串,以前判断亲和串的时候直接可以看出来,但现在不同了,现在给出的两字符串都非常的大,看的zyc头都晕了.于是zyc希望大家能帮他想一个办法来快速判断亲和串.亲和串定义:给定两个字符串s1和s2,如果能通过s1循环移动,使s2包含在s1中,那么我们就说s2是s1的亲和串. 输入 本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的