poj2752:KMP的简单应用

Seek the Name, Seek the Fame

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16388 Accepted: 8330

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

Source

POJ Monthly--2006.01.22,Zeyuan Zhu

题意:

求出前缀与后缀相同的情况,从小到大输出他们的长度。

做法:

KMP next(fail)数组的简单应用,从next[len]一直递推到底,输出长度即可。

next数组递推过程中一定是严格递减的,所以我们可以不用排序,直接逆序输出即可。

代码如下。

/*Source Code

Problem: 2752       User: aclolicon
Memory: 3188K       Time: 110MS
Language: G++       Result: Accepted
Source Code*/
#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXL 400050
using namespace std;
int fail[MAXL], len, ans, tot;
int a[MAXL];
char c[MAXL];

void getfail(){

    int i = 0, j = -1;
    fail[0] = -1;
    while(i != len){
        if (j == -1 || c[i] == c[j]) fail[++i] = ++j;
        else j = fail[j];
    }
}

int main(){
    while(~scanf("%s", c)){
        ans = 0, tot = 0;
        len = strlen(c);
        getfail();
        ans = fail[len];
        a[tot++] = len; //整个字符串肯定是
        while(ans > 0){
            a[tot++] = ans;
            ans = fail[ans];
        }
        for (int i = tot - 1; i >= 0; i--) cout << a[i] << (i == 0? ‘\n‘: ‘ ‘);
    }
}
时间: 2024-12-29 05:37:50

poj2752:KMP的简单应用的相关文章

(KMP 1.1)hdu 1711 Number Sequence(KMP的简单应用——求pattern在text中第一次出现的位置)

题目: Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12902    Accepted Submission(s): 5845 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1

KMP 算法简单解释

KMP 算法简单解释 ? 讲KMP算法,离不开BF,实际上,KMP就是BF升级版,主要流程和BF一样,就是在削除回溯上花了点功夫,利用Next数组来削除 <( ̄︶ ̄)[GO!] 1. 先看看BF算法(暴力破解) int Brute_force_1(const char *S, const char *T) { if (!S || !T) return -1; int lenS = strlen(S); int lenT = strlen(T); int i = 0; //主串下标索引 int j

POJ-2752(KMP算法+前缀数组的应用)

Seek the Name, Seek the Fame POJ-2752 本题使用的算法还是KMP 最主要的片段就是前缀数组pi的理解,这里要求解的纸盒pi[n-1]有关,但是还是需要使用一个循环来依次找到前面符合的前缀(所谓符合就是可以保持既是前缀也是s的后缀的子串长度). #include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<set>

KMP算法简单回顾

前言 虽从事企业应用的设计与开发,闲暇之时,还是偶尔涉猎数学和算法的东西,本篇根据个人角度来写一点关于KMP串匹配的东西,一方面向伟人致敬,另一方面也是练练手,头脑风暴.我在自娱自乐,路过的朋友别太认真,嘿 背景 目标串: T(1…..n) 模式串: P(1…..m) 输出:搜索P在T中的位置 s,令 T(s…s+m-1) === P(1…m) 例如: a g c t a g c a g c t a g c t g中查找a g c t g 返回 12(从1计数) 资料 资料太多了,我在此不准备进

hdu 2087(KMP的简单运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8742    Accepted Submission(s): 5722 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一

poj--3080Blue Jeans KMP的简单应用

穷举第一个字符串的所有子串,然后再判断其是否是其它字符串的子串. 然后注意输出字典序最小的答案. 判断枚举的子串是不是其它字符串子串时可以使用KMP,其实也可以直接暴力,因为题目数据范围不大. 学到一个技巧:可以使用memset(str,'\0',sizeof(str)将字符数组清空. 还有一点需要注意的是在自己组合的字符串后面一定要记得加上字符串结束标志'\0'. 代码如下: #include<iostream> #include<cstdio> #include<cstr

POJ2752(KMP)

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

MP and KMP

MP 是 KMP 的简单版本,目前以下题目都是MP算法 KMP的原理就不多说了 http://kb.cnblogs.com/page/176818/ (这个模板起始的next数组值为0,不是-1,在模板中,next数组叫f数组) #include <stdio.h> #include <stdlib.h> #include <string.h> #define maxn XXX//自己定 char P[maxn] ; int f[maxn] ; void getFail

KMP算法变形——对链表的处理

KMP算法简单粗暴的代码,严密的逻辑,初学的时候,真的很难搞懂,不过曾力胜老师这周出的模式串匹配的变形题目,让我反思了一下KMP算法,昨天晚上写出了链表形式,也算是进步吧.昨天太急,没来得及记录,今天补充起来. /* Name: KMP之链表写法 Date :2015/3/29 Write by:杨领 */ #include<stdio.h> #include<stdlib.h> typedef struct node { int data;//数据域 struct node *n