题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3973 , 线段树 + 字符哈希,好题。

  又学了一种新的哈希方法,hhhh~


解法:

  想法是用P进制的数来表示一个字符串,由于可能数太大,所以就将转换成是十进制后的数模long long的最大值,这样虽然也有可能冲突,但是概率会非常小。这里的P可以随意取一个素数(我取的是31)。

  先用上面提到的哈希方法将W集合中的字符串都转成十进制数存在数组中,然后进行排序;每一次询问时候,将询问区间的子串转成十进制后二分查找是否在W集合中即可。

   思路虽然简单,但是实现起来还是比较麻烦,尤其是有很多细节需要注意。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = + ;
const int p = ;
char str[];
LL Hash[maxn << ] , P[maxn];
LL W[ + ];
void init()
{
P[] = ;
for(int i = ; i <= maxn ; i++)
P[i] = P[i - ] * p;
}
LL calhash(char str[])
{
LL sum = ;
for(int i = ; str[i] != '\0' ; i++) {
sum = sum * p + str[i] - 'a' + ;
}
return sum;
}
int binary_search(int l , int r , LL a[] , LL x)
{
int m = (l + r) >> ;
while(l <= r) {
if(a[m] == x)
return m;
if(a[m] > x)
r = m - ;
if(a[m] < x)
l = m + ;
m = (l + r) >> ;
}
return -;
}
void PushUp(int l , int r , int rt)
{
int m = (l + r) >> ;
Hash[rt] = Hash[rt << ] * P[r - m] + Hash[rt << | ];
}
void build(int l , int r , int rt)
{
if(l == r) {
Hash[rt] = str[r] - 'a' + ;
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
PushUp(l , r , rt);
}
void update(int x , int l , int r , int rt)
{
if(l == r) {
Hash[rt] = str[x] - 'a' + ;
return;
}
int m = (l + r) >> ;
if(x > m)
update(x , rson);
else
update(x , lson);
PushUp(l , r , rt);
}
LL query(int L , int R , int l , int r , int rt)
{
if(L <= l && R >= r) {
return Hash[rt];
}
int m = (l + r) >> ;
if(m < L)
return query(L , R , rson);
else if(m >= R)
return query(L , R , lson);
else
return query(L , m , lson) * P[R - m] + query(m + , R , rson);
}
int main()
{
int n , m , T;
char ch[] , s[];
init();
cin >> T;
for(int k = ; k <= T ; k++)
{
scanf("%d" , &n);
for(int i = ; i <= n ; i++) {
scanf("%s" , str);
W[i] = calhash(str);
}
sort(W + , W + n + );
scanf("%s" , str);
int len = strlen(str);
build( , len - , );
printf("Case #%d:\n" , k);
scanf("%d" , &m);
while(m--) {
scanf("%s" , ch);
if(ch[] == 'Q') {
int L , R;
scanf("%d %d" , &L , &R);
LL tmp = query(L , R , , len - , );
if(binary_search( , n , W , tmp) != -)
puts("Yes");
else
puts("No");
} else {
int x;
scanf("%d" , &x);
scanf("%s" , s);
str[x] = s[];
update(x , , len - , );
}
}
}
return ;
}

HDU3973 线段树 + 字符哈希的更多相关文章

  1. 【URAL 1989】 Subpalindromes(线段树维护哈希)

    Description You have a string and queries of two types: replace i'th character of the string by char ...

  2. CF213E Two Permutations 线段树维护哈希值

    当初竟然看成子串了$qwq$,不过老师的$ppt$也错了$qwq$ 由于子序列一定是的排列,所以考虑插入$1$到$m$到$n-m+1$到$n$; 如何判断呢?可以用哈希$qwq$: 我们用线段树维护哈 ...

  3. [bzoj2124]等差子序列——线段树+字符串哈希

    题目大意 给一个1到N的排列\(A_i\),询问是否存在\(p_i\),\(i>=3\),使得\(A_{p_1}, A_{p_2}, ... ,A_{p_len}\)是一个等差序列. 题解 显然 ...

  4. 51Nod1553 周期串查询 字符串 哈希 线段树

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1553.html 题目传送门 - 51Nod1553 题意 有一个串只包含数字字符.串的长度为n,下标 ...

  5. 线段树+哈希【CF580E】Kefa and Watch

    线段树+哈希[CF580E]Kefa and Watch Description \(n\)个数的字符串,\(m + k\)个操作 1 l r k把\(l - r\)赋值为\(k\) 2 l r d询 ...

  6. 【Vjudge】P1989Subpalindromes(线段树)

    题目链接 水题一道,用线段树维护哈希值,脑补一下加减乱搞搞……注意细节就过了 一定注意细节…… #include<cstdio> #include<cstdlib> #incl ...

  7. 【线段树哈希】「Balkan OI 2016」Haker

    1A海星 题目大意 给你一个长度为 $n$ ,由小写字母构成的字符串 $S$ 和 $Q$ 个操作,每个操作是以下 3 种之一: 1 x y k :询问当前字符串从位置 $x$ 到 $y$ 的子串与从位 ...

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

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

  9. N - Subpalindromes URAL - 1989 哈希+线段树

    N - Subpalindromes URAL - 1989 这个是一个哈希+线段树,这个题目也不算特别难,但是呢,还比较有意思. 这个题目给你两个操作,一个是回答l~r 区间是不是回文,一个是对一个 ...

随机推荐

  1. 谈谈asp.net中的<% %>,<%= %>,<%# %><%$ %>的使用-转

    首先我们来看一下<% %>的使用 在aspx的页面中只能使用服务器控件和一般的控件,有些时候你想在该页面写入c#代码,必须使用<% %>,然后在里面写入c#的代码,下面我们来看 ...

  2. mysql失效的几种情况

    1.如果查询条件中有or,即使查询的条件中带有索引也会失效,如果想使用or,又不想让索引失效,只能将or条件中的所有列都加上索引 2.like 查询一%开头用不上索引, 3.隐式转换会使索引失效 比如 ...

  3. Jmeter如何提取响应头部的JSESSIONID【转】

    一.测试前准备 1.测试地址: 登录:http://XXXX:8080/futureloan/mvc/api/member/login 充值:http://XXXX:8080/futureloan/m ...

  4. 在JAVA中自定义连接数据库的工具类

    为什么要自定义数据库连接的工具类: 在开发中,我们在对数据库进行操作时,必须要先获取数据库的连接,在上一篇随笔中提到的获取数据库连接的步骤为: 1.定义好4个参数并赋值 2.加载驱动类 3.获取数据库 ...

  5. asddf

    https://docs.saltstack.com/en/getstarted/fundamentals/index.html https://pypi.org/simple/cherrypy/ 安 ...

  6. Swift几行代码解决UITableView空数据视图问题

    tableView空数据问题 一般项目中tableView若数据为空时会有一个提示示意图 为了更好的管理这种提示示意图,笔者利用extension进行了简单的拓展 解决思路 利用swift面向协议的特 ...

  7. P2308 添加括号(区间DP)

    题目背景 给定一个正整数序列a(1),a(2),...,a(n),(1<=n<=20) 不改变序列中每个元素在序列中的位置,把它们相加,并用括号记每次加法所得的和,称为中间和. 例如: 给 ...

  8. 2017 ACM/ICPC Asia Regional Shenyang Online number number number

    题意:求n个斐波那契数列组合都无法得到的最小数字 解法: 1 我们先暴力的求出前面几个数字 2 然后再暴力的求递推 3 接着矩阵快速幂(没写错吧?) /*#include<bits/stdc++ ...

  9. rabbitMq解析

    import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util ...

  10. python3.6下安装wingIDE破解方法

    1.wingIDE的下载: 在电脑配置好的python环境情况下,去官网下载wingIDE6,按照一般方式安装好.安装好它会自动提示你是否激活,你点击激活.然后到下一步. 2.脚本的制作: impor ...