AC dreamoj 1011 树状数组+hash维护字符串的前缀和
来源:互联网 发布:阿里数据分析师面试 编辑:程序博客网 时间:2024/06/07 22:55
http://acdream.info/problem?pid=1019
Problem Description
Now we have a long long string, and we will have two kinds of operation on it.
C i y : change the ith letter to y.
Q i j : check whether the substring from ith letter to jth letter is a palindrome.
Input
There are multiple test cases.
The first line contains a string whose length is not large than 1,000,000.
The next line contains a integer N indicating the number of operations.
The next N lines each lines contains a operation.
All letters in the input are lower-case.
Output
For each query operation, output "yes" if the corresponding substring is a palindrome, otherwise output "no".
Sample Input
aaaaa4Q 1 5C 2 bQ 1 5Q 1 3
Sample Output
yesnoyes
/**ACdreamoj 1019 树状数组+hash维护字符串的前缀和题目大意: 给定一个字符串,单点更新,区间查询该区间是不是回文串。解题思路: hash是x1 * p^1+ x2*p^2 +x3*p^3...可以用树状数组维护前缀和,维护两个串,一个是正串,另一个是反串用于比较。字符串区间s[l~r]的哈希值为sum(s[r]-s[l-1])/Hash[l-1];*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;typedef unsigned long long LL;const int maxn=1000010;const int seed=13;LL Hash[maxn],C[2][maxn];char s[maxn];int len;void init(){ Hash[0]=1; for(int i=1;i<maxn;i++) Hash[i]=Hash[i-1]*seed;}int lowbit(int x){ return x&(-x);}void add(int i,int x,LL pos){ while(x<=len) { C[i][x]+=pos; x+=lowbit(x); }}LL sum(int i,int x){ LL ans=0; while(x) { ans+=C[i][x]; x-=lowbit(x); } return ans;}LL gethash(int i,int l,int r){ return sum(i,r)-sum(i,l-1);}int main(){ init(); while(~scanf("%s",s+1)) { memset(C,0,sizeof(C)); len=strlen(s+1); for(int i=1;i<=len;i++) { add(0,i,(s[i]-'a')*Hash[i]); add(1,len+1-i,(s[i]-'a')*Hash[len+1-i]); } int T; cin >> T; while(T--) { char c[5]; scanf("%s",c); if(c[0]=='C') { char b[5]; int a; scanf("%d%s",&a,b); add(0,a,(b[0]-s[a])*Hash[a]); add(1,len+1-a,(b[0]-s[a])*Hash[len+1-a]); s[a]=b[0]; } else { int l,r; scanf("%d%d",&l,&r); if(gethash(0,l,r)*Hash[len-r]==gethash(1,len+1-r,len+1-l)*Hash[l-1])//采用交叉相乘把除换成了乘 puts("yes"); else puts("no"); } } } return 0;}
0 0
- AC dreamoj 1011 树状数组+hash维护字符串的前缀和
- ACdreamoj 1011(树状数组维护字符串hash前缀和)
- LA 4329 树状数组(BIT) 维护更新一段区间的前缀和
- BZOJ 3529 莫比乌斯反演 + 离线 + 树状数组动态维护前缀和
- [SDOI2009][BZOJ1878][前缀和][树状数组]HH的项链
- bzoj2124 等差子序列 (树状数组 维护hash值)
- 区间信息的维护和查询系列算法-树状数组
- Acdream 1019 Palindrome 树状数组 + 字符串hash
- hdu 5147 树状数组+前缀和
- HDU 5480(前缀和||树状数组)
- hdu 5157(manacher+前缀和+树状数组)
- 【POJ2155】Matrix-二维树状数组+前缀和
- LA4329 Ping pong 树状数组前缀和
- 前缀和维护
- bzoj 2124 神奇的树状数组+hash
- CS R25 C(BFS+二维前缀和) D(好题,合理枚举+树状数组维护) E(建图,连通分量,割点)
- HDU5147 Sequence II(树状数组+前缀和+后缀和)
- Codeforces 731D 求所有区间的交(树状数组或前缀和)
- mongodb 修改字段
- Nginx 常见应用技术指南(Nginx Tips)
- 生产者消费者问题理解与Java实现
- VIM用指定编码保存文件
- java jdbc 连接 mysql 数据库, mysql 实现 查询 指定行
- AC dreamoj 1011 树状数组+hash维护字符串的前缀和
- AABB和OBB包围盒简介
- LeetCode:Best Time to Buy and Sell Stock
- 第三章队列知识导图
- java 学习书籍-想精通java的必看
- Swift创建UIButton
- 线程和进程的区别
- 信跟几十万人一起成
- DUILIB配置