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

点击打开题目

Seek the Name, Seek the Fame

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12651   Accepted: 6214

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
大致题意:给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀。从小到大依次输出这些子串的长度
解题思路:重在理解KMP算法中next数组的含义,递归求解

#include<cstdio>
#define maxn 400002
char str[maxn];
int next[maxn],len,s;
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;
        }else j=next[j];
    }
    len=i;
}
void GetVal(int n){
    if(next[n]==0) return ;
    GetVal(next[n]);
    printf("%d ",next[n]);
}
int main(){
    while(scanf("%s",str)==1){
        GetNext();
        GetVal(len);
        printf("%d\n",len);
    }return 0;
}
时间: 2024-08-01 18:32:39

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

poj 2406 Power Strings【KMP】

点击打开题目 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33548   Accepted: 13935 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 =

算法题:poj 2752 Seek the Name, Seek the Fame(理解KMP的失配函数!)

链接: http://poj.org/problem?id=2752 题目大意: 给一个字符串S, 求出所有前缀pre,使得这个前缀也正好是S的后缀. 输出所有前缀的结束位置. 例如 "ababcababababcabab", 以下这些前缀也同时是S的后缀 ab  :  位置2 abab  : 位置4 ababcabab : 位置9 ababcababababcabab : 位置 18 分析与总结: 这题,关键在于对KMP的失配函数的理解.只要真正理解了,那么做出来完全不成问题. 下面

POJ 2752 C++ (KMP)

#include<iostream> #include<string> using namespace std; int n,next[400008],result[400008];; char s[400008],t[400008]; void Get_next() {int j,k; j=1; k=0; next[1]=0; while(j<=n+1) { if(k==0 || s[j]==s[k]) { j++; k++; next[j]=k; } else k=nex

POJ 2752

//KMP,对vector单个赋值不懂,只能用c语言形式拉 //大致题意:字符串s有多少个子串既是前缀又是后缀 #include <iostream> #include <string> #include <cstring> #include <cstdlib> using namespace std; const int N = 400010; int next[N] = {0},a[N] = {0}; void get_next(string s) {

poj 3167 Cow Bowling【dp】

这是一道最最基础的dp题目,还记得当时看刘汝佳写的<入门经典>时,就是拿的这个做例子,不过牛人当然是一笔带过这么简单的例子. 状态转移方程为:dp[i][j]+=max(dp[i+1][j],dp[i+1][j+1]),如果要记录路径也简单,另外再用一个数组专门存放原始数组就好 原三角形是 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 dp三角形是 30 23 21 20 13 10 7 12 10 10 4  5    2   6    5 如果要记录路径,就是从dp[1][1

poj 3264 Balanced Lineup【RMQ】

这道题是poj上面RMQ最具代表性的一道题,没有之一 题目也基本上就是一个裸RMQ的算法,看到这道题也可以用线段树解决,决定还是要出一个线段树的版本的,一道题搞懂两个知识点,多好! RMQ(Range Minimum/Maximum Query)问题:      RMQ问题是求给定区间中的最值问题.当然,最简单的算法是O(n)的,但是对于查询次数很多(设置多大100万次),O(n)的算法效率不够.可以用线段树将算法优化到O(logn)(在线段树中保存线段的最值).不过,Sparse_Table算

poj 2019 Cornfields【RMQ】

这道题是poj上一道还比较有名的二维RMQ (Range Minimum/Maximum Query 局部最大最小查询)问题,但是可能是这道题的数据设计的不是很好,所以可以直接暴力过,先上暴力AC版本的代码,之后会贴出RMQ正经版的代码... RMQ问题(区间询问最值问题). 题目大意:给出一个N*N矩形,每个格子上有一个价值.询问一个b*b的矩形在左上角的位置(x,y),(x+b-1,y+b-1)这一部分的最大值-最小值是多少. 题目分析:询问次数很多,但是矩形只有250且是正方形.二维的RM

【OJ】贪心法 Fence Repair POJ 3253 霍夫曼(Huffman)编码原理 acmclub 12326

题目链接:点击打开链接 /* 贪心法 Fence Repair POJ 3253 霍夫曼(Huffman)编码原理 */ #include<iostream> #include<algorithm> typedef long long LL; using namespace std; int l[50010]; int main(){ int j,m=0,n;cin>>n; LL s=0,ans=0; for(int i=0;i<n;i++)cin>>

【OJ】贪心法 Saruman&#039;s Army POJ 3069 /acmclub 12132

题目链接:点击打开链接 /* 6 10 贪心法Saruman's Army POJ 3069 1 7 15 20 30 50 ans=3 */ #include<iostream> #include<algorithm> using namespace std; int x[1010]; int main(){ // freopen("贪心法 Saruman's Army poj3069.txt","r",stdin); int n,r; w