POJ 2751:Seek the Name, Seek the Fame(Hash)
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;
}
POJ 2751:Seek the Name, Seek the Fame(Hash)的更多相关文章
- 总结:Mac前端开发环境的搭建(配置)
新年新气象,在2016年的第一天,我入手了人生中第一台自己的电脑(大一时好友赠送的电脑在一次无意中烧坏了主板,此后便不断借用别人的或者网站的).macbook air,身上已无分文...接下来半年的房 ...
- iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView
iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView 时间:2016-01-19 19:13:43 阅读:630 评论:0 收藏:0 ...
- jQuery:详解jQuery中的事件(二)
上一篇讲到jQuery中的事件,深入学习了加载DOM和事件绑定的相关知识,这篇主要深入讨论jQuery事件中的合成事件.事件冒泡和事件移除等内容. 接上篇jQuery:详解jQuery中的事件(一) ...
- GJM :用JIRA管理你的项目(三)基于LDAP用户管理 [转载]
感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...
- GJM :用JIRA管理你的项目(二)JIRA语言包支持及插件支持 [转载]
感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...
- Waves:类Material Design 的圆形波浪(涟漪)点击特效插件
Waves:类Material Design 的圆形波浪(涟漪)点击特效插件 2014/08/06 分类:前端开发, 素材分享 浏览:6,734次 来源:原创 1个评论 6,734 ...
- [你必须知道的.NET] 第八回:品味类型---值类型与引用类型(上)-内存有理
原文地址:http://kb.cnblogs.com/page/42318/ 系列文章导航: [你必须知道的.NET] 开篇有益 [你必须知道的.NET] 第一回:恩怨情仇:is和as [你必须知道的 ...
- Android Priority Job Queue (Job Manager):后台线程任务结果传回前台(三)
Android Priority Job Queue (Job Manager):后台线程任务结果传回前台(三) 在附录文章4,5的基础上改造MainActivity.java和MyJob.ja ...
- Android Priority Job Queue (Job Manager):线程任务的容错重启机制(二)
Android Priority Job Queue (Job Manager):线程任务的容错重启机制(二) 附录文章4简单介绍了如何启动一个后台线程任务,Android Priority J ...
随机推荐
- ie edge 自动给数字加下划线
<meta name="format-detection" content="telephone=no,email=no,address=no">
- jsp标签之jsp:setProperty用法
参考:http://blog.csdn.net/wanghaishanren/article/details/2047400
- Saiku二次开发获取源代码在本地编译(五)
关于Saiku的二次开发,在本地编译然后启动自己编译好的Saiku服务 Saiku是开源的,从github上能下载源代码,本例中的saiku源码也是从github上找的,然后自己改了一些pom.xml ...
- sqlalchemy 模型中添加根据身份证号计算性别和年龄
class Users(Base, BaseMixin): username = Column(String(24)) id_Num = Column(String(18) ) # 身份证号码 @pr ...
- 深入理解java虚拟机---虚拟机工具jstat(十七)
jstack---没什么用 jstack用于生成java虚拟机当前时刻的线程快照.线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目的是定位线程出现长时间停顿的原因 ...
- Android开发---如何操作资源目录中的资源文件
效果图: 1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Linear ...
- Oracle非归档模式下脱机数据文件
正常情况下,要想对数据文件脱机,必须在归档模式下,这是ORACLE自动保护的一种措施,防止在非归档模式下对数据文件脱机,造成数据丢失.如果想在非归档模式下执行数据文件脱机操作,则需要加上“for dr ...
- restful接口设计规范总结
这篇 文章主要是借鉴他人,但是自己很想总结出一套规范,以供向我这样的新手使用,用来规范代码,如果有什么好的提议,请不吝赐教,本篇文章长期更新! 一.重要概念: REST,即Representation ...
- http 性能测试. Apache ab 使用.
参数: 1. ab -n 100 -c 10 地址: 请求100次, 并发10次. 2. ab -n 100 -c 10 -w 地址: 请求100次, 并发10次 ,html 表 ...
- dynamic programming:find max subarray
public static mark dynway( ) //put or pop the books into the basket,mark the highest recorder { int ...