【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 ... 
随机推荐
- Send Boxes to Alice
			E. Send Boxes to Alice 首先求出每一个位置的前缀和. 对答案进行复杂度为\(\sqrt{a[n]}\)的遍历,因为最后的答案不可能大于\(\sqrt{a[n]}\) for(ll ... 
- League of Leesins
			C - League of Leesins 首先找到每一串数字的头和尾两个数字,这两个数字有一个特点,就是它们在输入数据的时候都只会出现一次.我们在输出的时候用头和尾做第一数都可以. 然后第二个数只会 ... 
- python3 django连接mysql数据库
			在django中将模型类中的数据迁移到mysql数据库中,首先使用pip install pymysql安装pymysql库, 然后在项目中的__init__.py中添加 import pymysql ... 
- Linux -bash: redis-cli: command not found(亲测可行)
			Linux 安装完redis单独用命令: ? 1 redis-server 报错: ? 1 -bash: redis-server: command not found 说明redis-server不 ... 
- 数据库中的几个概念 - LGWR, ARCH,ASYNC,SYNC,AFFIRM
			双机热备(双机容错)就是对于重要的服务,使用两台服务器,互相备份,共同执行同一服务.当一台服务器出现故障时,可以由另一台服务器承担服务任务,从而在不需要人工干预的情况下,自动保证系统能持续提供服务 双 ... 
- Kotlin入门-Android的基础布局
			线性布局线性布局LinearLayout是最常用的布局,顾名思义,它下面的子视图像是用一根线串了起来,所以其内部视图的排列是有顺序的,要么从上到下垂直排列,要么从左到右水平排列.排列顺序只能指定一维方 ... 
- Python日志模块应用
			# encoding:utf-8 import logging import time class Logs: def __init__(self): self.logger = logging.ge ... 
- 16 Flutter仿京东商城项目 跳转到搜索页面实现搜索功能 以及搜索筛选
			ProductList.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.dart'; imp ... 
- CentOS7做ssh免密登录
			(1)实验环境 两台CentOS7: youxi1 192.168.1.6 youxi2 192.168.1.7 这里我将防火墙关闭进行实验,如果防火墙开启,请将端口加入到防火墙规则中. (2).目标 ... 
- Python操作memecache
			memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载,故常用来做数据库缓存.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态 ... 
