HDU 3973

通过哈希函数将一个字符串转化为一个整数,通过特定的方式可以使得这个哈希值几乎没有冲突(这就是关键之处,几乎没有视为没有= =!, 其实也可以考虑实现哈希冲突时的处理,只是这道题没必要而已),然后使用线段树维护修改后的哈希值。

因为输入的字符串只有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 字符串哈希的更多相关文章

  1. HDU - 3973 AC's String(Hash+线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3973 题意 给一个词典和一个主串.有两种操作,查询主串某个区间,问这主串区间中包含多少词典中的词语.修改主串某一 ...

  2. [HDU 4821] String (字符串哈希)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 题目大意:给你M,L两个字母,问你给定字串里不含M个长度为L的两两相同的子串有多少个? 哈希+枚 ...

  3. hdu 4300 Clairewd’s message 字符串哈希

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. 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 ...

  6. 牛客练习赛33 E tokitsukaze and Similar String (字符串哈希hash)

    链接:https://ac.nowcoder.com/acm/contest/308/E 来源:牛客网 tokitsukaze and Similar String 时间限制:C/C++ 2秒,其他语 ...

  7. hdu3973 AC's String 线段树+字符串hash

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3973/ 题意是:给出一个模式串,再给出一些串组成一个集合,操作分为两种,一种是替换模式串中的一个字符,还有一种是 ...

  8. HDU 1880 魔咒词典(字符串哈希)

    题目链接 Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...

  9. HDU 5842 Lweb and String(Lweb与字符串)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

随机推荐

  1. java基础类型中的char和byte的辨析及Unicode编码和UTF-8的区别

    在平常工作中使用到char和byte的场景不多,但是如果项目中使用到IO流操作时,则必定会涉及到这两个类型,下面让我们一起来回顾一下这两个类型吧. char和byte的对比 byte byte 字节, ...

  2. Redis快速入门之简介

    一.Redis是什么? Redis 是一个开源(BSD许可)基于内存数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如字符串(strings), 散列(hashes) ...

  3. 逆向及BOF基础实践

    逆向及BOF基础实践 20145316 许心远 一.缓冲区溢出基础知识 缓冲区溢出是一种非常普遍.非常危险的漏洞,在各种操作系统.应用软件中广泛存在.利用缓冲区溢出攻击,可以导致程序运行失败.系统宕机 ...

  4. autofac 在webapi中拿到当前request的scope

    https://stackoverflow.com/questions/31321386/autofac-web-api-get-current-scope   Unless you are usin ...

  5. tfn2k工具使用介绍

    主要介绍tfn2k(Linux),因为它最著名嘛!主要分为使用说明 然后在说安装所以有点长 (注意:有的人拿VPS来做DDOS·问题是有的人发现了会关闭你VPS的·除非你认识他或者你自己有服务器而不是 ...

  6. shell脚本监控Linux系统性能指标

    2016-11-04 22:41 原作者不详 分类: Linux(7) 在服务器运维过程中,经常需要对服务器的各种资源进行监控, 例如:CPU的负载监控,磁盘的使用率监控,进程数目监控等等,以在系统出 ...

  7. 往前端打smarty数据

    $data['hot_issue']=$hotIssue; var_dump($data);

  8. python fromkeys() 创建字典

    # d = dict.fromkeys("张无忌","赵敏") #创建字典 # print(d)#{'张': '赵敏', '无': '赵敏', '忌': '赵敏 ...

  9. 《F4+2 团队项目需求分析改进》

    a.分析<动态的太阳系模型项目需求规格说明书>初稿的不足. 任务概述描述的有些不具体,功能的规定不详细,在此次作业进行了修改. b.参考<构建之法>8.5节功能的定位和优先级, ...

  10. 自己喜欢用的一个初始化的common.css

    body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, form, fieldset, input, p, blockquote ...