1. 程式人生 > >POJ 2752 -kmp求所有公共前後綴長度

POJ 2752 -kmp求所有公共前後綴長度

題目連結:

描述:

給出一字串,求所有公共前後綴長度,從小到大輸出,顯然字串長度也為一個公共前後綴,且為最長的

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陣列,abcab ……… abcab 最後一個字元的next值為以該字母結束的最長前後綴長度,再到公共綴中找,公共前後綴,必然是整個串的公共前後綴

這裡寫圖片描述

/*************************************************************************
    > File Name: poj_2752.cpp
    > Author: dulun
    > Mail: [email protected]
    > Created Time: 2016年03月21日 星期一 12時25分35秒
 ************************************************************************/

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define LL long long
using namespace std;

const int N = 400009;
char a[N];
int nxt[N];
int sum[N];

void get_nxt()
{
    int k = 0; 
    int l = strlen(a);
    memset(nxt, 0, sizeof(nxt));
    for(int i = 1; i < l; i++)
    {
        while(k && a[i] != a[k]) k = nxt[k-1];
        if(a[i] == a[k]) k++;
        nxt[i] = k;
    }
}

int main()
{
    while(scanf("%s", a) != EOF)
    {
        get_nxt();
        int l = strlen(a);

        memset(sum, 0, sizeof(sum));      
        int k = 0;
        int m = l;
        while(m)
        {
            sum[k++] = nxt[m-1];
            m = nxt[m-1];
        }
        for(int i = k-2; i>=0; --i) printf("%d ", sum[i]);
        printf("%d\n", l);
    }

    return 0;
}

相關推薦

POJ 2752 kmp所有公共前後長度

題目連結: 描述: 給出一字串,求所有公共前後綴長度,從小到大輸出,顯然字串長度也為一個公共前後綴,且為最長的 Description The little cat is

HDU 4763 Theme Section(KMP+枚舉公共前後)

break 前綴 cstring namespace hdu oid tdi href mp算法 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=4763 題目大意: 給你一個字符串s,存在一個子串E同時出現在前綴、中間

POJ 2752 Seek the Name, Seek the Fame(KMP公共前後

字符串 iostream 字符 org 所有 對稱性 char can for 題目鏈接:http://poj.org/problem?id=2752 題目大意:給你一串字符串s找到所有的公共前後綴,即既是前綴又是後綴的子串。 解題思路: 如圖所示 假設字

poj 3461 【KMP模式串在匹配串中的出現次數 】

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 from th

Codeforces Round #545 (Div. 2)D(KMP,最長公共前後,貪心)

strlen lse ros ++ clu str 足夠 force 標記 #include<bits/stdc++.h>using namespace std;const int N=1000007;char s1[N],s2[N];int len1,len2

Codeforces 506 Div3.A 尋找最長公共前後

A. Many Equal Substrings time limit per test 1 second memory limit per test 256 megabytes input standard input output standard o

poj 2752 Seek the Name, Seek the Fame (KMP前後相同)

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2

poj 2752 kmp 前後

Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24500 Accepted: 12787 Descripti

poj--3450 KMP多個字串的最長公共子串

思路與前面的3080一樣 程式碼如下: #include<iostream> #include<cstdio> #include<cstring> using n

POJ 3450 Corporate Identity(kmp多個字串的最長公共子串)

http://poj.org/problem?id=3450 #include <stdio.h> #include <string.h> const int max_N=

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

baby body key out single clas ble numbers ask Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissi

POJ 3376 Finding Palindromes(manacher前後回文串+trie)

!= pri min ret 長度 ems algo 邊緣 字符串 題目鏈接:http://poj.org/problem?id=3376 題目大意:給你n個字符串,這n個字符串可以兩兩組合形成n*n個字符串,求這些字符串中有幾個是回文串。 解題思路:思路參考了這裏:h

HDU 3613 Best Reward(擴展KMP前後回文串)

ref gpo namespace .cn pan spa pos tdi kmp 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 題目大意: 大意就是將字符串s分成兩部分子串,若子串是回文串則需計算價值,否則

POJ 2406 - Power Strings - [KMP最小循環節]

abcd for each 題解 blog 分享圖片 %d power exp clas 題目鏈接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Give

POJ 2752 Seek the Name, Seek the Fame(KMP的next陣列)

【連結】http://poj.org/problem?id=2752 【題意】給個字串,求這個串所有字首與字尾相同的所有綴長度 【思路】 其實kmp裡的next這個微妙的陣列本身就是答案了,至於為什麼,我是用以前不知道哪裡弄來的的kmp模板試了一下看出來的【喂

H - Seek the Name, Seek the Fame POJ - 2752(找到所有相同的字首和字尾)

題目連結http://poj.org/problem?id=2752 題意: 給你一個字串str  找到一個字串集合S  ,集合內的字串都為str的字首和字尾     思路: 如果這個字串s在這個集合的話,那麼s肯定是字串str的一

Seek the Name, Seek the Fame POJ - 2752 (KMP 失配指標陣列)

題意:     給定一個串T,找出串T的子串,該串即既是T的字首也是T的字尾.從小到大輸出所有符合要求的子串的長度. 分析:  把答案就是f[m]  f[ f[m] ]...依次下去.理解f陣列的字首字尾思想這道題就迎刃而解了. &

HDU2594 (kmp兩字串的前後相同長度最大,水題)

    Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.  Marge: Yeah, what is it?&nbs

poj 2774 字尾陣列 最長連續公共子串長度

Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 35480 Accepted: 14218 Case Time Limit: 10

Dijkstra--POJ 2502 Subway(所有路徑再最短路徑)

題意: 你從家往學校趕,可以用步行和乘坐地鐵這兩種方式,步行速度為10km/h,乘坐地鐵的速度為40KM/h。輸入資料的第一行資料會給你起點和終點的x和y的座標。然後會給你數目不超過200的雙向地鐵線路的站點,你可以從一個站點乘坐地鐵到下一個站點,你可以在同一線