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 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的后缀。从小到大依次输出这些子串的长度。

对于所定义的next数组(我用的p数组),                                                        --()--

a  a  a   a  a                                    --------()---------

-1  0  1   2   3  4   next 数组              ----------------------()

根据next数组的组成:  如图:

首先其记录串长为i=5,然后用循环求next[i]并记录其值,即可得解!

代码如下:

#include<stdio.h>
#include<string.h>
const int max=400010;
char str[max],ch[max];
int ans[max],p[max],len,len1;
void Getp()//取出p数值(即next数组)
{
    int i=0,j=-1;
    p[i]=j;
    while(i<len)
    {
        if(j==-1||str[i]==str[j])
        {
            i++;j++;
            p[i]=j;
        }
        else
        j=p[j];
     }
    }
int main()
{
   while(scanf("%s",str)!=EOF)
   {
       int i,j=1;
       len=strlen(str);
       Getp();
       i=len;
       while(p[i]!=0)//循环求p数组(即next数组)
       {
           ans[j++]=p[i];
           i=p[i];
    }
     ans[0]=len;
     for(i=j-1;i>0;i--)
      {
          printf("%d ",ans[i]);
      }printf("%d\n",ans[0]);
   }
   return 0;
}

原题目:Seek the Name, Seek the Fame
 poj 2752

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 12:24:27

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

[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)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(next运用)

题目大意:小猫是非常有名气的,所以很多父母都来找它给孩子取名字,因为找的人比较多,小猫为了摆脱这个无聊的工作,于是它发明了一种取名字的办法,它把孩子父母的名字合在一起,然后从这个名字里面找一个前缀,并且这个前缀也得是后缀,然后用它当孩子的名字,比如父亲的名字是:ala,母亲的名字是la, 那么孩子的名字就可以是“a”,“ala”,“alala”, 现在想知道孩子的名字可以多长?   分析:最长的那个一定是字符串的长度,第二长的就是前缀后缀的最大匹配度 next[N],第三长的就是next[nex

Seek the Name, Seek the Fame POJ - 2752(拓展kmp)

题意: 就是求前缀和后缀相同的那个子串的长度  然后从小到大输出 解析: emm...网上都用kmp...但...我..用拓展kmp做的  这就是拓展kmp板题嘛... 求出extend数组后  把extend[i] == len - i 的放到vector中 最后排序输出就好了 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <m

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

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,

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

POJ 2752 Seek the Name, Seek the Fame kmp失配函数next应用

点击打开链接 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12791   Accepted: 6304 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give

POJ 2752 Seek the Name, Seek the Fame (KMP的next函数运用)

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