ural1989(字符串hash+线段树)
来源:互联网 发布:js 自定义日期选择器 编辑:程序博客网 时间:2024/05/16 15:41
这题困扰了我一天,在timus上用G++交总是超时,后来改用visualc++2010就ac了,不知道为什么,求大牛解释。
题意:给一个长度为n串,两种操作:1、修改一个字符;2、询问区间[l,r]上的字符串是否为回文串。
先介绍一下字符串hash:将某个字符串用一个正整数代替。现假设只有小写字母的串,一个有26个字母,比如aabb,将其转化成0011,然后将其转化成一个26进制的数字。
思路:长度是100000的26进制数,这肯定会溢出,但没关系,我们用unsigned long long 就可以了,让它自动溢出即可。然后某个数字就对应一个字符串,虽然有些不同串对应同一个数字,但是概率非常小,可以忽略不计。从左到右、从右到左进行两次hash,如果是回文串,那么对应的整数必定存在某种关系(可以理解成相等),对于更新操作,就是单点更新。
代码如下:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<stdio.h>#include<stdlib.h>#include<math.h>#define N 100005#define LL unsigned long long#define inf 0x7ffffff#define eps 1e-9#define pi acos(-1.0)using namespace std;LL f[N];char s[N];int n;struct node{ int l,r; LL suml,sumr;//记录从左到右和从右到左的hash值}tree[4*N];void pushup(int o){ tree[o].suml = tree[2*o].suml + tree[2*o+1].suml; tree[o].sumr = tree[2*o].sumr + tree[2*o+1].sumr;}void build(int o,int l,int r){ tree[o].l = l; tree[o].r = r; if(l == r) { tree[o].suml = f[l-1]*(s[l-1] - 'a'); tree[o].sumr = f[n-l]*(s[l-1] - 'a'); return ; } int m = (l+r)/2; build(2*o,l,m); build(2*o+1,m+1,r); pushup(o);}void update(int o,int pos,int num){ if(tree[o].l == tree[o].r){ tree[o].suml = f[ tree[o].l - 1 ]*num; tree[o].sumr = f[ n-tree[o].l ]*num; return ; } int m = (tree[o].l+tree[o].r)/2; if(pos <= m) update(2*o,pos,num); else update(2*o+1,pos,num); pushup(o);}LL suml,sumr;void query(int o,int x,int y){ if(x <= tree[o].l && tree[o].r <= y) { suml += tree[o].suml; sumr += tree[o].sumr; return; } int m = (tree[o].l+tree[o].r)/2; if(m >= x) query(2*o,x,y); if(y > m) query(2*o+1,x,y);}int main(){//freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout); int i; f[0] = 1; for(i = 1; i < N; i++) f[i] = f[i-1]*27; while(scanf("%s",s) != EOF) { n = strlen(s); build(1,1,n); int q; scanf("%d",&q); while(q--) { scanf("%s",s); if(s[0] == 'p') { int x,y; scanf("%d%d",&x,&y); suml = sumr = 0; query(1,x,y); int k1 = x-1; int k2 = n-y; if(k1 > k2) sumr *= f[k1-k2];//这里自己举个例子想一下 else suml *= f[k2-k1]; if(suml == sumr) printf("Yes\n"); else printf("No\n"); } else{ int x; scanf("%d%s",&x,s); update(1,x,s[0]-'a'); } } } return 0;}
0 0
- ural1989(字符串hash+线段树)
- 线段树 + 字符串Hash
- hdu 4622 Reincarnation(字符串Hash+线段树)
- URAL 1989 Subpalindromes(字符串HASH&线段树单点更新)
- URAL 1989 Subpalindromes(线段树单点修改+字符串hash)
- bnu36907 Subpalindromes 字符串hash+线段树
- hdu 3973 字符串hash+线段树
- hdu 3973 AC's String(字符串hash+线段树单点修改)
- 【线段树+字符串hash】 codeforces 213E Two Permutations
- hdu 3973 字符串hash+线段树动态维护
- codeforces 321# E. Kefa and Watch (线段树+字符串hash)
- codeforces 580 E. Kefa and Watch (字符串hash + 线段树)
- 字符串hash+线段树 Codeforces580E Kefa and Watch
- Codeforces Round #321 (Div. 2)E 线段树+字符串hash
- [字符串Hash 线段树] AOJ 2734 Donut Decoration
- Hash练习题:【213E】 Two Permutations(线段树,hash)
- 线段树+hash+codeforces213E
- hdu 3973(hash+线段树)
- 在myeclipse中代码修改不起作用:当在myeclipse中修改处理方法的代码时,重新发布Tomcat,在运行时,jsp页面显示内容没变,还是按修改前的代码执行
- 2014-10深圳全球架构师峰会
- 605662545微信公众号可以做广告了
- 大数的加减乘除(转赵老四)
- Radar Installation
- ural1989(字符串hash+线段树)
- box filtering
- 605662545--win7 64位旗舰版系统的几种必须重装的情况
- 分类器评价指标
- HDU 2813 One fihgt one(KM最大匹配)
- 深圳MPD大会 讲师演讲稿 2014-10
- Unity3d使用json与java服务器通信
- matlab求矩阵的鞍点
- UVA - (单点BFS , 多源点同时加入queue)