【hash】Seek the Name, Seek the Fame
【哈希和哈希表】Seek the Name, Seek the Fame
题目描述
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:)
输入
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
输出
样例输入
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的更多相关文章
- 【BZOJ1941】[Sdoi2010]Hide and Seek KDtree
[BZOJ1941][Sdoi2010]Hide and Seek Description 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了 ...
- 【hash】BZOJ3751-[NOIP2014]解方程
[题目大意] 已知多项式方程:a0+a1*x+a2*x^2+...+an*x^n=0.求这个方程在[1,m]内的整数解(n和m均为正整数). [思路] *当年考场上怒打300+行高精度,然而没骗到多少 ...
- 【hash】Power Strings
[题意]: 给出s串出来,能否找到一个前缀 ,通过多次前缀进行拼接.构成s串.如果有多个,请输出最多次数那个. 如:aaaa 可以用1个a,进行4次拼接 可以用2个a,进行2次拼接 可以用4个a,进行 ...
- 【hash】Similarity of Subtrees
图片来源: https://blog.csdn.net/dylan_frank/article/details/78177368 [题意]: 对于每一个节点来说有多少对相同的子树. [题解]: 利用层 ...
- 【hash】A Horrible Poem
[题目链接] # 10038. 「一本通 2.1 练习 4」A Horrible Poem [参考博客] A Horrible Poem (字符串hash+数论) [题目描述] 给出一个由小写英文字母 ...
- 【hash】Three friends
[来源]:bzoj3916 [参考博客] BZOJ3916: [Baltic2014]friends [ 哈希和哈希表]Three Friends [Baltic2014][BZOJ3916]frie ...
- 湖南师范大学2018年大学生程序设计竞赛新生赛 A 齐神和心美的游戏【hash】
[链接]:A [题意]:给你n个数的序列和k.判断是否可以三个数组成k(同一个数可以拿多次) [分析]:每个数vis记录一下.2层循环.两数之和不超过k以及剩下的数出现在序列中那么ok. [代码]: ...
- 【hash】珍珠
[来源] https://loj.ac/problem/2427 [参考博客] LOJ#2427. 「POI2010」珍珠项链 Beads [题解]: 复杂度计算: 暴力枚举k每次计算是n/2+n/3 ...
- 【bzoj1941】 Sdoi2010—Hide and Seek
http://www.lydsy.com/JudgeOnline/problem.php?id=1941 (题目链接) 题意 给出n个二维平面上的点,求一点使到最远点的距离-最近点的距离最小. Sol ...
随机推荐
- 启动maven项目时报错Failed to start component [StandardEngine[Tomcat]]: A child container failed during start
详细错误信息:Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run (default-cli) on ...
- [Tex学习笔记]让项目编号从4开始
微信扫描如上二维码关注跟锦数学微信公众账号. 详情请见那里.
- 如何单独编译Linux内核的某个模块?
1. 配置该模块为[M] 2. 编译 make modules SUBDIRS=./drivers/rtc (5.3的内核为make modules M=./drivers/rtc) 3. 安装 ma ...
- kotlin之lambda表达式和匿名函数
lambda表达式,称为匿名函数,是一种函数字面值,也就是没有声明的函数,但可以作为表达式传递出去. 函数类型: 对于接受另一个函数的作为自己的参数,必须针对这个参数指定一个函数的类型如 fun &l ...
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_23-页面预览-页面预览开发
1.用户进入cms前端,点击“页面预览”在浏览器请求cms页面预览链接. 2.cms根据页面id查询DataUrl并远程请求DataUrl获取数据模型. 3.cms根据页面id查询页面模板内容 4.c ...
- Spring Cloud(6.1):搭建OAuth2 Authorization Server
配置web.xml 添加spring-cloud-starter-security和spring-security-oauth2-autoconfigure两个依赖. </dependency& ...
- wp-query调用前几篇文章的方法
---恢复内容开始--- 利用强大的wp-query函数调用指定分类下的前几篇文章,下面的代码表示调用的是分类ID4下的前两篇文章. <?php $cunt_wenzhen = array('c ...
- DOTS默认情况下的性能
利用Unity全新的高性能多线程数据导向技术堆栈(DOTS),充分利用当今的多核处理器.您的游戏运行速度更快,您的代码更易于在其他项目中阅读和重用. 重建Unity的核心 我们正在使用高性能多线程数据 ...
- XSS 攻击的预防
XSS 攻击有两大要素: 1.攻击者提交恶意代码. 2.浏览器执行恶意代码. 针对第一个要素:我们是否能够在用户输入的过程,过滤掉用户输入的恶意代码呢? 输入过滤 在用户提交时,由前端过滤输入,然后提 ...
- cvpr2015papers
@http://www-cs-faculty.stanford.edu/people/karpathy/cvpr2015papers/ CVPR 2015 papers (in nicer forma ...