Seek the Name, Seek the Fame

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24077   Accepted: 12558

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

题意

给出一个字符串ch,求出ch中存在多少子串,使得这些子串既是ch的前缀,又是ch的后缀。从小到大依次输出这些子串的长度。

思路

这个题好像很多都是用KMP写的,百度看了看代码,递归的那个过程不是太理解,就用Hash写了,感觉Hash的思路更简单

记得要提前预处理一下

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
const int base=2333;
using namespace std;
char ch[maxn];
ull a[maxn];
ull b[maxn];
int vis[maxn];
int main(int argc, char const *argv[])
{
a[0]=1;
for(int i=1;i<maxn;i++)
a[i]=a[i-1]*base;
while(cin>>(ch+1))
{
ms(b);
int l=strlen(ch+1);
b[1]=ch[1];
for(int i=2;i<=l;i++)
b[i]=b[i-1]+a[i-1]*ch[i];
int k=0;
for(int i=1;i<=l;i++)
{
if(b[i]*a[l-i]==(b[l]-b[l-i]))
vis[k++]=i;
}
for(int i=0;i<k;i++)
{
if(i)
cout<<" ";
cout<<vis[i];
}
cout<<endl;
}
return 0;
}

最新文章

  1. 介绍一款原创的四则运算算式生成器:CalculateIt2
  2. Eclipse “cannot be resolved to a type” 错误
  3. Java类与对象的基础学习
  4. 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest C. CIA Datacenter
  5. uva 11728 Alternate Task
  6. leetcode majority number
  7. 面试体验:Facebook 篇(转)
  8. 【其他】IT公司的企业文化与竞争力
  9. ThinkPHP框架搭建及常见问题(Apache或MySQL无法启动)----简单的初体验
  10. HTTP协议分析
  11. ops-web运维平台data.jsp-jquery-mootools
  12. MQ队列与哪些机器连接
  13. 数据结构之哈希(hash)表
  14. Linux 最小化安装后IP的配置(DHCP获取IP地址)
  15. PowerDesigner使用技巧(转载)
  16. ADOX创建ACCESS 表时,几个附加属性
  17. Hyperledger Fabric 1.0 从零开始(一)
  18. 【转】word排版宏的使用
  19. VS2012 创建 WebService
  20. cocos2dx 3.0 编译工程

热门文章

  1. js问题 项目问题
  2. Spring @Scheduled @Async联合实现调度任务(2017.11.28更新)
  3. vim : Depends: vim-common (= 2:7.4.052-1ubuntu3.1) but 2:7.4.273-2ubuntu4 is to be installed
  4. Java反射《三》获取属性
  5. Windows系统上设置 Git Bash 的 Font 及 Locale
  6. tensorflow-可视化
  7. 杭电多校第四场 E Matrix from Arrays
  8. PCP架构设计
  9. 火狐下,td 的 bug;
  10. Oracle 用户 表 表空间之间的关系和管理