HDU 3973 AC's String 字符串哈希
通过哈希函数将一个字符串转化为一个整数,通过特定的方式可以使得这个哈希值几乎没有冲突(这就是关键之处,几乎没有视为没有= =!, 其实也可以考虑实现哈希冲突时的处理,只是这道题没必要而已),然后使用线段树维护修改后的哈希值。
因为输入的字符串只有26个,考虑使用一个大于等于26的素数p作为进制,然后将原串转化为一个p进制的数mod 2^63(也相当于自然溢出),然后这个数存入map中,然后使用线段树维护长串区间的哈希值,hash[l, r]表示将区间[l, r]的字符串转化为p进制数(哈希过程)的结果,那么对于当前区间[l, r]的两个子区间[l, m]与[m + 1, r]:
hash[l, r] = hash[l, m] + hash[m + 1, r] * (p ^ (m - l + 1))
使用线段树动态维护,查询区间的hash值,使用map查找,那么问题就解决了。
//#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, (L + R)>>1
#define rson k<<1|1, ((L + R)>>1) + 1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define dec(i, a, b) for(int i = a; i >= b; i --) template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = + ;
const int MAXM = ;
const double eps = 1e-;
LL MOD = ; const LL P = ; int t, n, m, cas = , l, r;
LL mi[MAXN];
char str[], ch;
map<LL, int>mp; void init() {
mi[] = ;
rep (i, , MAXN - ) {
mi[i] = mi[i - ] * P;
}
} int toNum(char ch) {
return ch - 'a' + ;
} LL Hash() {
LL ans = ;
int len = strlen(str);
dec (i, len - , ) {
ans = ans * P + toNum(str[i]);
}
return ans;
} struct SegTree {
LL ma[MAXN << ]; void update(int k, int L, int R, int p, int v) {
if(L == R) { ma[k] = v; return ; }
int mid = (L + R) >> ;
if(mid >= p) update(lson, p, v);
else update(rson, p, v);
ma[k] = ma[k << ] + ma[k << | ] * mi[mid - L + ];
} LL query(int k, int L, int R, int l, int r) {
if(r < L || R < l) return ;
if(l <= L && R <= r) return ma[k];
LL le = query(lson, l, r), ri = query(rson, l, r);
int mid = (L + R) >> ;
return le + ri * mi[max(mid - max(L, l) + , )];
} }st; int main()
{
// FIN;
init();
cin >> t;
while(t--) {
mp.clear();
mem0(st.ma);
scanf("%d%*c", &n);
rep (i, , n - ) {
scanf("%s", str);
mp[Hash()] = ;
}
scanf("%s", str);
int len = strlen(str);
rep (i, , len - ) {
st.update(, , len, i + , toNum(str[i]));
}
printf("Case #%d:\n", ++cas);
scanf("%d%*c", &m);
while(m --) {
scanf("%c", &ch);
if(ch == 'Q') {
scanf("%d %d%*c", &l, &r);
printf("%s\n", mp[st.query(, , len, ++l, ++r)] ? "Yes" : "No");
}
else {
scanf("%d %c%*c", &l, &ch);
st.update(, , len, ++l, toNum(ch));
}
}
}
return ;
}
HDU 3973 AC's String 字符串哈希的更多相关文章
- HDU - 3973 AC's String(Hash+线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=3973 题意 给一个词典和一个主串.有两种操作,查询主串某个区间,问这主串区间中包含多少词典中的词语.修改主串某一 ...
- [HDU 4821] String (字符串哈希)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 题目大意:给你M,L两个字母,问你给定字串里不含M个长度为L的两两相同的子串有多少个? 哈希+枚 ...
- hdu 4300 Clairewd’s message 字符串哈希
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- PAT 甲级 1047 Student List for Course (25 分)(cout超时,string scanf printf注意点,字符串哈希反哈希)
1047 Student List for Course (25 分) Zhejiang University has 40,000 students and provides 2,500 cou ...
- 牛客练习赛33 E tokitsukaze and Similar String (字符串哈希hash)
链接:https://ac.nowcoder.com/acm/contest/308/E 来源:牛客网 tokitsukaze and Similar String 时间限制:C/C++ 2秒,其他语 ...
- hdu3973 AC's String 线段树+字符串hash
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3973/ 题意是:给出一个模式串,再给出一些串组成一个集合,操作分为两种,一种是替换模式串中的一个字符,还有一种是 ...
- HDU 1880 魔咒词典(字符串哈希)
题目链接 Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...
- HDU 5842 Lweb and String(Lweb与字符串)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
随机推荐
- 解决 failed to push some refs to 'git@github.com:zle1992/head-first-java' hint: Updates were rejected because the tip of your curr
问题描述: 寒假之前用实验室电脑push到github 上head first java 的程序,寒假回家后,想用自己的笔记本继续编,继续push . 我先从github下载zip到本地,然后 解压后 ...
- AVAudioFoundation(3):音视频编辑
本文转自:AVAudioFoundation(3):音视频编辑 | www.samirchen.com 本文主要内容来自 AVFoundation Programming Guide. 音视频编辑 上 ...
- Linux学习笔记之Linux最小化安装启动后如何配置
在VM虚拟机中安装CentOS 7 时 有时候顾虑到电脑硬件性能,我们需要最小化安装,而最小化安装后与centos6的版本是有一些差异的,接下来我们就对刚安装好的最小化centos7做一些操作,来世我 ...
- SQL学习笔记三(补充-1)之MySQL存储引擎
阅读目录 一 什么是存储引擎 二 mysql支持的存储引擎 三 使用存储引擎 一 什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 现实生活中我们用来存储数据的 ...
- Python3.x:open()文件操作
Python3.x:open()文件操作 open/文件操作: #open(路径+文件名,读写模式) #读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 f=ope ...
- nw.js node-webkit系列(18)怎么对.js进行编译以防你的代码暴露出来
原文链接:http://blog.csdn.net/zeping891103/article/details/50819102 参考:https://segmentfault.com/a/119000 ...
- 【联网】虚拟机下Linux(终端)配置网络的方法
这几天在虚拟机vmware上部署centos系统,想通过内部联网用yum命令安装必需的软件,但是一直不能静态地址联网,今天终于找到一个方法centos内部设置IP,对外联网.设置过程如下: 1.首先是 ...
- [QT]QApplication和QCoreApplication的用法
转自:http://www.tuicool.com/articles/qmI7Bf 故事的背景是这样的,我们在写QT程序的时候或者在开始写QT程序之前总会看到这样的语句 QApplication ap ...
- Java中泛型区别以及泛型擦除详解
一.引言 复习javac的编译过程中的解语法糖的时候看见了泛型擦除中的举例,网上的资料大多比较散各针对性不一,在此做出自己的一些详细且易懂的总结. 二.泛型简介 泛型是JDK 1.5的一项新特性,一种 ...
- 递归--练习11--noi9273 PKU2506Tiling
递归--练习11--noi9273 PKU2506Tiling 一.心得 25 a[i]%=10;(高精度时) 26 这里错了,花了好久改好 27 28 29 int* f(int n){ 30 if ...