Seek the Name, Seek the Fame
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24390   Accepted: 12723

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

  1. ababcababababcabab
  2. aaaaa

Sample Output

  1. 2 4 9 18
  2. 1 2 3 4 5

Source

题意:求一个串中满足前缀等于后缀的子串的长度
这里要跟回文串区别开来,回文是从前往后和从后往前完全匹配;
而这个是前面一部分与后面一部分完全匹配。
题解:kmp中求出的next数组,本身就是具有这个结构,不过next数组中
求出的是当前的最大前缀等于后缀的前缀子串的位置。
那么只要递归输出这个next数组就行了。
代码:
  1. #include<iostream>
  2. #include<string.h>
  3. #include<string>
  4. #include<stdlib.h>
  5. #include<algorithm>
  6. #include<stdio.h>
  7. #include<queue>
  8. using namespace std;
  9. typedef long long ll;
  10. typedef unsigned long long ull;
  11. typedef long long ll;
  12. typedef pair<int,int> PII;
  13. #define mod 1000000007
  14. #define pb push_back
  15. #define mp make_pair
  16. #define all(x) (x).begin(),(x).end()
  17. #define fi first
  18. #define se second
  19. //head
  20. #define MAX 400005
  21. int next[MAX];
  22. char s[MAX];
  23. int len;
  24. void get_next()
  25. {
  26. int i=,j=-;
  27. next[i]=j;
  28. for(i=;i<len;i++)
  29. {
  30. while(j>-&&s[j+]!=s[i])
  31. j=next[j];
  32. if(s[j+]==s[i]) j++;
  33. next[i]=j;
  34. }
  35. }
  36. void output(int x)
  37. {
  38. if(next[x]==-)
  39. {
  40. printf("%d ",x+);
  41. return ;
  42. }
  43. output(next[x]);
  44. printf("%d ",x+);
  45. //cout<<"ok"<<endl;
  46. }
  47. int main()
  48. {
  49. /*ios_base::sync_with_stdio(0);
  50. cin.tie(0);
  51. cout.tie(0);*/
  52. while(~scanf("%s",s))
  53. {
  54. memset(next,,sizeof(next));
  55. len=strlen(s);
  56. get_next();
  57. /*for(int i=0;i<len;i++)
  58. cout<<next[i]<<" ";
  59. cout<<endl;*/
  60. output(len-);
  61. printf("\n");
  62. }
  63. return ;
  64. }
 
 
 
 
 
 
 
 
 

poj2752Seek the Name, Seek the Fame(next数组)的更多相关文章

  1. poj2752seek the name, seek the fame【kmp】

    The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...

  2. poj2752Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  3. POJ 2752Seek the Name, Seek the Fame(next数组妙用 + 既是前缀也是后缀)

    题目链接 题意:求一个字符串中 前缀 和 后缀 相同的长度 分析: 对于一个字符串他自己的长度肯定是可以的.然后如果满足 前缀 和 后缀相等,那个前缀 最后一个字符 一定 和 该字符串最后一个字符相等 ...

  4. POJ 2752 Seek the Name, Seek the Fame(next数组的理解)

    做此题,只要理解好next数组就行....................... #include <cstdio> #include <cmath> #include < ...

  5. POJ--2752--Seek the Name, Seek the Fame【KMP】

    链接:http://poj.org/problem? id=2752 题意:对于一个字符串S,可能存在前n个字符等于后n个字符,从小到大输出这些n值. 思路:这道题加深了对next数组的理解.next ...

  6. POJ 2752 Seek the Name, Seek the Fame next数组理解加深

    题意:给你一个字符串,寻找前缀和后缀相同的子串(包括原串). 从小到大排列输出其子串的长度 思路:KMP  next 数组应用. 其实就是一个数学推导过程. 首先由next数组 可知s(ab) = s ...

  7. poj2752 Seek the Name, Seek the Fame(next数组的运用)

    题目链接:id=2752" style="color:rgb(202,0,0); text-decoration:none; font-family:Arial; font-siz ...

  8. POJ 2752 Seek the Name, Seek the Fame(next数组运用)

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

  9. POJ2752 Seek the Name, Seek the Fame —— KMP next数组

    题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Li ...

随机推荐

  1. zabbix 微信告警脚本

    #!/usr/bin/env python3 import requests import json import sys import os def access_token(Corpid,Secr ...

  2. Linux筛选数据

    grep命令:在文件中查找关键字,并显示所在行(Globally search a Regular Expression and Print) grep text file:在file文件中搜索tex ...

  3. Center os 用户环境变量

    vi ~/.bash_profile进入用户环境变量设置 export JAVA_HOME=/usr/java/jdk1.7.0_76export JAVA_BIN=$JAVA_HOME/binexp ...

  4. 【串线篇】SQL映射文件-resultMap自定义封装

    mybatis默认封装规则: 1).按照列明和属性名一一对应的规则(不区分大小写) 2).如果不一一对应: 1).开启驼峰命名(数据库aaa_bbb, 程序中aaaBbb) 2).起别名 3).自定义 ...

  5. SPOJ1693 COCONUTS - Coconuts

    传送门[洛谷] 自闭QAQ 什么玩意QAQ 不是很理解到底在干啥 问了巨佬以后大概是这个样子的 可以看出是最小割模型 对于每一个人 反悔的话就是代价+1 那么连接(s,i) (i,t)分别表示他最后选 ...

  6. 使用字符流(Writer、Reader)完成对文件的读写操作

    字符流 字符输出流:Writer,对文件的操作使用子类FileWriter 字符输入流:Reader,对文件的操作使用子类FileReader 每次操作的是一个字符 文件字符操作流会自带缓存,默认大小 ...

  7. setInterval,setTimeout,clearInterval

    定时器 var i=0; function iadd(){ i++; console.log(i) } setInterval(iadd,1000);//1.2.3... 超时调用 var i=0; ...

  8. zoj 3325 Machine(线段树)

    题意:0~n-1的数组,初始值为0:执行m个操作,每次操作执行后输出当前值为0的连续段的段数. 操作1: p i j : i~j区间的每个元素值减1 操作2: r i j :i~j区间的每个元素值加1 ...

  9. JS 判断undefined

    tax !== underfined underfined 是判断的是类型的结果, 如果加typeof后是字符串类型 写法:typeof(tax) !== "underfined" ...

  10. auto_now_add与auto_now的区别