码迷,mamicode.com
首页 > 其他好文 > 详细

[POJ2752]Seek the Name, Seek the Fame

时间:2015-08-25 21:31:43      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

  题目链接:http://poj.org/problem?id=2752

 

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

 

本题求的是整个字符串中前后缀相同的子串,输出的是他们的长度。

通过这个题又加深了对KMP的pre数组的认识:

  pre可以认为是(如pre[n] = k)字符串0-n中前k个字符和后k个字符相同。即前缀和后缀相同。

  n != 0,我们可以认为只要pre[n] != 0,那么就存在前后缀相同的子串,长度恰好是k。

  (PS:“遍历”的过程有点像并查集)

   PPS:注意输出要按照"increasing number"

代码如下:

 

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cmath>
 7 #include <queue>
 8 #include <map>
 9 #include <stack>
10 #include <list>
11 #include <vector>
12 
13 using namespace std;
14 
15 const int maxn = 400010;
16 
17 int nb;
18 char b[maxn];
19 int pre[maxn];
20 
21 void getpre(char *b, int *pre) {
22     int j, k;
23     pre[0] = -1;
24     j = 0;
25     k = -1;
26     while(j < nb) {
27         if(k == -1 || b[j] == b[k]) {
28             j++;
29             k++;
30             pre[j] = k;
31         }
32         else {
33             k = pre[k];
34         }
35     }
36 }
37 
38 int main() {
39     // freopen("in", "r", stdin);
40     while(gets(b) && strlen(b)) {
41         int ans[maxn];
42         int cnt = 0;
43         nb = strlen(b);
44         getpre(b, pre);
45         while(pre[nb] != -1) {
46             // printf("%d ", nb);
47             ans[cnt++] = nb;
48             nb = pre[nb];
49         }
50         for(int i = cnt-1; i >= 0; i--) {
51             printf("%d ", ans[i]);
52         }
53         printf("\n");
54     }
55 }

 

[POJ2752]Seek the Name, Seek the Fame

标签:

原文地址:http://www.cnblogs.com/vincentX/p/4758339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!