N - Subpalindromes URAL - 1989 哈希+线段树
N - Subpalindromes
这个是一个哈希+线段树,这个题目也不算特别难,但是呢,还比较有意思。
这个题目给你两个操作,一个是回答l~r 区间是不是回文,一个是对一个点进行单点修改。
这个用哈希还是比较好用的,首先就是把所有字符映射成一个数字,然后就相当于给你一串数字进行以上操作。
这个最好就是从小到大进行一个递增的哈希,这个看代码吧,说不太清楚。
主要注意一点就是最后要保证这个哈希的位数是一样的,具体看代码。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + ;
const ull base = ;
ull h[maxn], p[maxn];
ull sum1[maxn * ], sum2[maxn * ];
int n, m;
char str1[maxn], str2[maxn]; void push_up(int id) {
sum1[id] = sum1[id << ] + sum1[id << | ];
sum2[id] = sum2[id << ] + sum2[id << | ];
//printf("sum[%d]=%llu nisum[%d]=%llu\n", id, sum[id], id, nisum[id]);
} void build(int id, int l, int r) {
if (l == r) {
sum1[id] = (str1[l] - 'a' + )*p[l];//哈希赋值,从小往大,递增赋值
sum2[id] = (str2[l] - 'a' + )*p[l];
return;
}
int mid = (l + r) >> ;
build(id << , l, mid);
build(id << | , mid + , r);
push_up(id);
} void update(int id, int l, int r, int pos, int val, int f) {
if (l == r) {
if (f == ) sum1[id] = val * p[l];
else sum2[id] = val * p[l];
return;
}
int mid = (l + r) >> ;
if (pos <= mid) update(id << , l, mid, pos, val, f);
else update(id << | , mid + , r, pos, val, f);
push_up(id);
} ull query(int id, int l, int r, int x, int y, int f) {
if (x <= l && y >= r) {
if (f == ) return sum1[id];
return sum2[id];
}
int mid = (l + r) >> ;
ull ans = ;
if (x <= mid) ans += query(id << , l, mid, x, y, f);
if (y > mid) ans += query(id << | , mid + , r, x, y, f);
return ans;
} char s[maxn], ch[]; int main() {
scanf("%s", str1 + );
n = strlen(str1 + );
p[] = ;
for (int i = ; i <= n; i++) p[i] = p[i - ] * base, str2[i] = str1[n + - i];
build(, , n);
scanf("%d", &m);
while (m--) {
int x, y;
scanf("%s", s);
if (s[] == 'p') {
scanf("%d%d", &x, &y);
ull rest1 = query(, , n, x, y, );//这样子查询还是有点难想的
ull rest2 = query(, , n, n + - y, n + - x, );
if (x < n + - y) rest1 *= p[n + - x - y];//这个if是保证这两个rest的数量级是一样的,这个可以画一个x轴就知道了
else rest2 *= p[x + y - n - ]; if (rest1 == rest2) printf("Yes\n");
else printf("No\n");
}
else {
scanf("%d%s", &x, ch);
update(, , n, x, ch[] - 'a' + , );
update(, , n, n - x + , ch[] - 'a' + , );
}
}
}
哈希
N - Subpalindromes URAL - 1989 哈希+线段树的更多相关文章
- 51Nod1553 周期串查询 字符串 哈希 线段树
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1553.html 题目传送门 - 51Nod1553 题意 有一个串只包含数字字符.串的长度为n,下标 ...
- URAL 1297 后缀数组+线段树
思路: 论文题--*n 倒过来接上 分奇偶讨论 求LCP 搞棵线段树即可 //By SiriusRen #include <cstdio> #include <cstring> ...
- 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)
原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解 By 岩之痕 目录: 一:综述 ...
- 【URAL 1989】 Subpalindromes(线段树维护哈希)
Description You have a string and queries of two types: replace i'th character of the string by char ...
- URAL 1989 Subpalindromes (多项式hash) +【线段树】
<题目链接> <转载于 >>> > 题目大意:给你一段字符串,进行两种操作:1.询问[l,r]这个区间中的字符串是否是回文串: 2.更改该字符串中对应下标的 ...
- 线段树+哈希【CF580E】Kefa and Watch
线段树+哈希[CF580E]Kefa and Watch Description \(n\)个数的字符串,\(m + k\)个操作 1 l r k把\(l - r\)赋值为\(k\) 2 l r d询 ...
- [bzoj2124]等差子序列——线段树+字符串哈希
题目大意 给一个1到N的排列\(A_i\),询问是否存在\(p_i\),\(i>=3\),使得\(A_{p_1}, A_{p_2}, ... ,A_{p_len}\)是一个等差序列. 题解 显然 ...
- CF213E Two Permutations 线段树维护哈希值
当初竟然看成子串了$qwq$,不过老师的$ppt$也错了$qwq$ 由于子序列一定是的排列,所以考虑插入$1$到$m$到$n-m+1$到$n$; 如何判断呢?可以用哈希$qwq$: 我们用线段树维护哈 ...
- HDU3973 线段树 + 字符哈希
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3973 , 线段树 + 字符哈希,好题. 又学了一种新的哈希方法,hhhh~ 解法: 想法是用P进制的数 ...
随机推荐
- java消除 list重复值及交集,并集,差集
消除 list重复值 Java代码 public void removeDuplicate(List list) { HashSet h = new HashSet(list); list.clea ...
- 010-字符串-C语言笔记
010-字符串-C语言笔记 学习目标 1.[掌握]二维数组的声明和初始化 2.[掌握]遍历二维数组 3.[掌握]二维数组在内存中的存储 4.[掌握]二维数组与函数 5.[掌握]字符串 一.二维数组的声 ...
- 深入理解new运算符
在 JavaScript 中,new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例.创建一个对象很简单,为什么我们还要多此一举使用 new 运算符呢?它到底有什么样的魔力? 认 ...
- Nikto使用方法
Introduction Nikto是一款开源的(GPL)网站服务器扫描器,使用Perl基于LibWhisker开发.它可以对网站服务器进行全面的多种扫描,包括6400多个潜在危险的文件/CGI,检查 ...
- 数据结构之循环队列Demo
循环队列 比较简单,循环队列主要是判断队满.队空.有效元素个数 画图说明: 假设:队的长度为5(0-4) 但是实际maxsize为6,需要一个预留空间(不存储元素)做计算 继续添加3个元素后: 出队一 ...
- B - How Many Equations Can You Find dfs
Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9.You are asked to add the sig ...
- PHP函数:array_rand
array_rand() - 从数组中随机取出一个或多个单元. 说明: array_rand ( array $array [, int $num = 1 ] ) : mixed 参数: array ...
- Linux学习笔记(四)帮助命令
帮助命令 man info help --help man 英文原意:format and display the on-line manual pages 功能:显示联机帮助手册 语法:man 选项 ...
- 浅析CopyOnWriteArrayList
CopyOnWriteArrayList引入 模拟传统的ArrayList出现线程不安全的现象 public class Demo1 { public static void main(String[ ...
- eclipse添加方法注释
打开注释模板编辑窗口:Window ->Preferences->java -> Code Style -> Code Template->Comments type 设 ...