rolling hash
也是需要查看,然后修改,rolling hash, recursive hash, polynomial hash, double hash.如果一次不够,那就2次。需要在准备一个线段树,基本的线段树容易些,带lazy标记的区间修改的线段树不是很好写。hash seed key根据需要选择, 我看别人写的,可以写成一个随机数,每次随机选择一个素数作为种子,这样好像好一些。
#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, n) for (int i = 0; i < (int)n; ++i)
#define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e6 + ;
const int mod1 = 1e9 + ;
const int mod2 = 1e9 + ;
const int arg1 = ;
const int arg2 = ;
int n, k;
string str;
pii H[maxn];
map<pii, int> has[maxn];
map<pii, int> g;
int pw1, pw2;
bool check(map<pii, int> & m) {
for (auto it = m.begin(); it != m.end(); it++) {
if(it->second > ) return ;
else if(g.find(it->first) == g.end()) return false;
}
return ;
}
void solve() {
cin >> n >> k;
cin >> str;
pw1 = pw2 = ;
for (int i = ; i < k - ; i++) {
pw1 = ll(pw1) * arg1 % mod1;
pw2 = ll(pw2) * arg2 % mod2;
}
pii h = pii(, );
for (int i = ; i < k; i++) {
h.first = (ll(h.first) * arg1 + ll(str[i] - 'a' + )) % mod1;
h.second = (ll(h.second) * arg2 + ll(str[i] - 'a' + )) % mod2;
}
H[] = h;
has[][h]++;
for (int i = ; i < n * k; i++) {
h.first = (ll(h.first) - ll(str[i - ] - 'a' + ) * pw1 % mod1 + mod1) % mod1;
h.second = (ll(h.second) - ll(str[i - ] - 'a' + ) * pw2 % mod2 + mod2) % mod2;
int ni = (i + k - ) % (n * k);
h.first = (ll(h.first) * arg1 + ll(str[ni] - 'a' + )) % mod1;
h.second = (ll(h.second) * arg2 + ll(str[ni] - 'a' + )) % mod2;
has[i % k][h]++;
H[i] = h;
}
int m; cin >> m;
string cur;
for (int j = ; j <= m; j++) {
cin >> cur;
pii h = pii(, );
for (int i = ; i < k; i++) {
h.first = (ll(h.first) * arg1 + ll(cur[i] - 'a' + )) % mod1;
h.second = (ll(h.second) * arg2 + ll(cur[i] - 'a' + )) % mod2;
g[h] = j;
}
}
for (int j = ; j < k; j++) if(check(has[j])) {
cout << "YES" << endl;
bool f = ;
for (int i = j; i < n * k; i += k) {
if(f) cout << " ";
cout << g[H[i]]; f = ;
}
cout << endl;
return;
}
cout << "NO" << endl;
}
int main() {
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
solve();
return ;
}
rolling hash的更多相关文章
- Rolling Hash(Rabin-Karp算法)匹配字符串
您可以在我的个人博客中访问此篇文章: http://acbingo.cn/2015/08/09/Rolling%20Hash(Rabin-Karp%E7%AE%97%E6%B3%95)%E5%8C%B ...
- Rolling Hash about the Rsync
今天看文献看到一个有趣的算法—Rolling Hash,这个算法可以更新在不同的machine上的两个“similar”的文件,也叫做rsync algorithm,rsync顾名思义:remote ...
- Hash function
Hash function From Wikipedia, the free encyclopedia A hash function that maps names to integers fr ...
- Hash算法入门指南(聊点不一样的算法人生)
前言 很多人到现在为止都总是问我算法该怎么学啊,数据结构好难啊怎么的,学习难度被莫名的夸大了,其实不然.对于一个学计算机相关专业的人都知道,数据结构是大学的一门必修课,数据结构与算法是基础,却常常容易 ...
- 几种常用hash算法及原理
计算理论中,没有Hash函数的说法,只有单向函数的说法.所谓的单向函数,是一个复杂的定义,大家可以去看计算理论或者密码学方面的数据.用“人 类”的语言描述单向函数就是:如果某个函数在给定输入的时候,很 ...
- 经典Hash函数的实现
Hash函数是指把一个大范围映射到一个小范围.把大范围映射到一个小范围的目的往往是为了节省空间,使得数据容易保存. 除此以外,Hash函数往往应用于查找上.所以,在考虑使用Hash函数之前,需要明白它 ...
- 应用Hash函数
本文系转载,转自:http://www.blogjava.net/jinfei0627/articles/219543.html 应用Hash函数(java描述) 作者:冲处宇宙 时间:2007.1. ...
- 几种经典的hash算法
计算理论中,没有Hash函数的说法,只有单向函数的说法.所谓的单向函数,是一个复杂的定义,大家可以去看计算理论或者密码学方面的数据.用“人 类”的语言描述单向函数就是:如果某个函数在给定输入的时候,很 ...
- 常见的哈希Hash算法 & MD5 & 对称非对称加密 & 海明码
参考 Link 另外,这篇文章也提到了利用Hash碰撞而产生DOS攻击的案例: http://www.cnblogs.com/charlesblc/p/5990475.html DJB的算法实现核心是 ...
随机推荐
- 算法:排序----Java选择排序
public static void selectionSort(int[] arr) { int len = arr.length; for (int i = 0; i < len; i++) ...
- xcode 左边导航栏中,类文件后面的标记“A”,"M","?"……等符号的含义???
"M" = Locally modified "U" = Updated in repository "A" = Locally a ...
- mha 自动failover 原创
自动failover slave1:stop slave io_thread slave2stop slave io_thread server1: create database sbtest; ...
- 如何在有实体键的情况下全部显示ActionBar的Menu?
大伙都知道, 在老版本手机, 以及部分的新手机上都还残留实体键, 有了这些实体键, 默认菜单是用实体菜单键呼出的, 尽管你把android:showAsAction="always" ...
- php执行效率相关的语句
一:字符替换: strtr > str_replace > preg_replace 注意: 1:一般用strtr函数的这种形式:string strtr ( string $str , ...
- linux下使用find xargs grep查找文件及文件内容
1,在某个路径下查文件. 在/etc下查找“*.log”的文件 find /etc -name “*.log” 2,扩展,列出某个路径下所有文件,包括子目录. find /etc -name “*” ...
- 最新搭建GIT服务器仓库
新开了一个项目,现在需要将代码放在公司GIT服务器上面.所以这里需要了一些问题..记录一下.因为原来公司这边的服务器的git用户都是创建好的.这里没有创建.需要的可以看看:http://www.cnb ...
- cocos2d-x中使用sqlite
在单机游戏中有几十个场景道具,每一个都有各自的状态(获得.未获得.获得个数)等等,如果在游戏中平凡涉及到这些道具的实时存储,那么使用文本就会稍慢.可以使用sqlite数据库来完成. 下载地址:http ...
- TCP/IP协议原理与应用笔记17:IP编址(重点)
1. IP地址(通用标识符) 对于同一个网络设备(主机或路由器)的不同网络连接,需要不同的IP地址进行标识 2. 主机标识符 主要有下面三种方式的主机标识方式: (1)Name:是什么,可读性强(了解 ...
- 加载程序到android虚拟机报错: android.widget.RelativeLayout cannot be cast to android.widget.Button
05-23 02:53:48.416: E/Trace(875): error opening trace file: No such file or directory (2) 05-23 02:5 ...