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 ...
随机推荐
- Java基础教程:IO流与文件基础
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
- HDU - 2844 Coins(多重背包+完全背包)
题意 给n个币的价值和其数量,问能组合成\(1-m\)中多少个不同的值. 分析 对\(c[i]*a[i]>=m\)的币,相当于完全背包:\(c[i]*a[i]<m\)的币则是多重背包,考虑 ...
- c# ArrayList 的排序问题!
2009-01-19 20:10 c# ArrayList 的排序问题! c# ArrayList 的排序问题! 我看见网上有人用IComparer接口实现ArrayLIst 的排序问题 ,于是自己写 ...
- JAVA volatile 解析
volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...
- bzoj1610 / P2665 [USACO08FEB]连线游戏Game of Lines
P2665 [USACO08FEB]连线游戏Game of Lines 第一次写快读没判负数....(捂脸) 暴力$O(n^2)$求斜率,排序判重. 注意垂直方向的直线要特判. end. #inclu ...
- (Matlab)GPU计算简介,及其与CPU计算性能的比较
1.GPU与CPU结构上的对比 2.GPU能加速我的应用程序吗? 3.GPU与CPU在计算效率上的对比 4.利用Matlab进行GPU计算的一般流程 5.GPU计算的硬件.软件配置 5.1 硬件及驱动 ...
- Qt5编译error01
1.Qt551x86 vs2013 1.1.“error: C2001: 常量中有换行符” 问题还原:源码:“QString str = tr("已接收 %1MB (%2MB/s) \n共% ...
- 使用H5 canvas画一个坦克
具体步骤如下: 1. 首先做出绘图区,作为坦克的战场 <canvas id="floor" width="800px" height=&quo ...
- AtomicLong可以被原子地读取和写入的底层long值的操作
java.util.concurrent.atomic.AtomicLong类提供了可以被原子地读取和写入的底层long值的操作,并且还包含高级原子操作. AtomicLong支持基础long类型变量 ...
- WPF——RenderTransform特效
WPF: RenderTransform特效 WPF中的变形(RenderTransform)类是为了达到直接去改变某个Silverlight对象的形状(比如缩放.旋转一个元素)的目的而设计的,Ren ...