【哈希和哈希表】Seek the Name, Seek the Fame

题目描述

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:)

输入

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. 

输出

For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.

样例输入

ababcababababcabab
aaaaa

样例输出

2 4 9 18
1 2 3 4 5

【题意】:

对于一个字符串s,找出所有相同的前缀后缀长度.

【题解】:

利用hash的方法,直接取两端的值。

代码是自己刚学hash时写的,所以有点乱,代码是参考wsy的。

 #include<bits/stdc++.h>
#define Mp make_pair
#define F first
#define S second
using namespace std;
const int N = 1e6+;
const int M1 = 1e9+ , M2 = 1e9+;
typedef long long ll;
typedef struct Node{
long long first ,second; Node (){}
Node ( ll u,ll v){ first = u , second = v ;}
}PII;
//typedef pair<ll,ll> PII; const PII base{M2,M1},p{M1,M2},One{1ll,1ll},Zero{0ll,0ll}; PII operator - (PII u,PII v){
return Node( (u.first-v.first+p.first)%p.first ,(u.second-v.second+p.second)%p.second );
}
PII operator * ( PII u , PII v ){
return Node( (u.first*v.first)%p.first , (u.second*v.second)%p.second );
}
PII operator + ( PII u , PII v ){
return Node( (u.first+v.first)%p.first , (u.second+v.second)%p.second );
}
PII operator + ( PII u , int v ){
return Node( (u.first+v)%p.first , (u.second+v)%p.second );
}
bool operator != ( PII u,PII v ){
return !( u.first == v.first && u.second == v.second );
}
bool operator == ( PII u,PII v ){
return ( u.first == v.first && u.second == v.second );
}
PII Pow( PII a ,int b){
PII ans = One ;
while( b ){
if( b& )
ans = ans * a ;
b >>= ;
a = a * a ;
}
return ans ;
}
PII sum[N];
char str[N];
int ans[N];
int main()
{
ios_base :: sync_with_stdio();
cin.tie(NULL),cout.tie(NULL); while( cin >> str+ ){
if( str[] == '.') break;
int len = strlen(str+);
int n = len ;
sum[n+] = Zero ;
for(int i=len;i>=;i--)
sum[i] = sum[i+] * base + str[i] ;
int cnt = ;
/*
for(int i=1;i<=n;i++){
printf("%lld %lld \n",sum[i].first,sum[i].second);
}
*/
PII P = base ;
for(int i=n-;i>=;i--){
//printf("#### %d \n",i);
if( sum[] - sum[+i] == P * sum[n-i+] ){
ans[cnt++] = n-i ;
}
P = P * base ;
//printf("%lld * %lld = %lld \n ",sum[i].first,Pow(base,n-i-1).first ,(sum[n]-sum[n-i]).first );
}
//sort( ans , ans + cnt );
for(int i=;i<cnt;i++){
cout << ans[i] << ' ';
}
cout << n << endl;
//cout << ans << endl ;
}
return ;
}

双哈希

【hash】Seek the Name, Seek the Fame的更多相关文章

  1. 【BZOJ1941】[Sdoi2010]Hide and Seek KDtree

    [BZOJ1941][Sdoi2010]Hide and Seek Description 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了 ...

  2. 【hash】BZOJ3751-[NOIP2014]解方程

    [题目大意] 已知多项式方程:a0+a1*x+a2*x^2+...+an*x^n=0.求这个方程在[1,m]内的整数解(n和m均为正整数). [思路] *当年考场上怒打300+行高精度,然而没骗到多少 ...

  3. 【hash】Power Strings

    [题意]: 给出s串出来,能否找到一个前缀 ,通过多次前缀进行拼接.构成s串.如果有多个,请输出最多次数那个. 如:aaaa 可以用1个a,进行4次拼接 可以用2个a,进行2次拼接 可以用4个a,进行 ...

  4. 【hash】Similarity of Subtrees

    图片来源: https://blog.csdn.net/dylan_frank/article/details/78177368 [题意]: 对于每一个节点来说有多少对相同的子树. [题解]: 利用层 ...

  5. 【hash】A Horrible Poem

    [题目链接] # 10038. 「一本通 2.1 练习 4」A Horrible Poem [参考博客] A Horrible Poem (字符串hash+数论) [题目描述] 给出一个由小写英文字母 ...

  6. 【hash】Three friends

    [来源]:bzoj3916 [参考博客] BZOJ3916: [Baltic2014]friends [ 哈希和哈希表]Three Friends [Baltic2014][BZOJ3916]frie ...

  7. 湖南师范大学2018年大学生程序设计竞赛新生赛 A 齐神和心美的游戏【hash】

    [链接]:A [题意]:给你n个数的序列和k.判断是否可以三个数组成k(同一个数可以拿多次) [分析]:每个数vis记录一下.2层循环.两数之和不超过k以及剩下的数出现在序列中那么ok. [代码]: ...

  8. 【hash】珍珠

    [来源] https://loj.ac/problem/2427 [参考博客] LOJ#2427. 「POI2010」珍珠项链 Beads [题解]: 复杂度计算: 暴力枚举k每次计算是n/2+n/3 ...

  9. 【bzoj1941】 Sdoi2010—Hide and Seek

    http://www.lydsy.com/JudgeOnline/problem.php?id=1941 (题目链接) 题意 给出n个二维平面上的点,求一点使到最远点的距离-最近点的距离最小. Sol ...

随机推荐

  1. Ubuntu验证查看库的安装情况

    以下是ubuntu系统安装完成一些库后,验证查看各个库的安装情况. 1. CUDA8.0 yuanlibin@yuanlibin:~$ nvcc -V nvcc: NVIDIA (R) Cuda co ...

  2. vs.net2017在编辑的wpf的xaml文件引用本程序集下的类提示“找不到”

    local对应就是当前exe程序下的类,会提示“...命令空间...找不到...” 因为我调整过生成的,于是尝试调回来anyCPU 问题解决. 看了一下vs.net2017的所在目录"C:\ ...

  3. php mvc 模式的开发注意事项

    1.控制器中: 如果不涉及到数据库的就在控制器中. empty($res['code']) ? $this->error($res['msg']) : $this->success($re ...

  4. H5本地存储详解

    H5之前存储数据一般是通过 cookie ,但是 cookie 存的数据容量比较少.H5 中扩充了文件存储能力,可存储多达 5MB 的数据.现在就实际开发经验来对本地存储 ( Storage ) 的使 ...

  5. 五一 DAY 5

    五一  DAY 5 V  点  1----n E  边 /* Given a graph with N nodes and M unidirectional edges. Each edge e_i ...

  6. 交叉编译多平台 FFmpeg 库并提取视频帧(转)

    交叉编译多平台 FFmpeg 库并提取视频帧 转  https://www.cnblogs.com/leviatan/p/11142579.html 本文档适用于 x86 平台编译 armeabi.a ...

  7. iOS获取应用当前Caches目录路径以及当前日期

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSSt ...

  8. 10Flutter页面布局 Padding Row Column Expanded组件详解:

    Padding组件: main.dart import 'package:flutter/material.dart'; import 'res/listData.dart'; /* flutter页 ...

  9. 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_10.RabbitMQ研究-工作模式-路由工作模式介绍

    队列在绑定交换机的时候可以指定routingKey, 路由模式: 1.每个消费者监听自己的队列,并且设置routingkey. 2.生产者将消息发给交换机,由交换机根据routingkey来转发消息到 ...

  10. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_12-删除页面-服务端-接口开发

    删除页面 api里面定义接口 返回类型是ReponseResult @ApiOperation("删除页面") public ResponseResult delete(Strin ...