URAL 1989. Subpalindromes
来源:互联网 发布:过山车大亨3 mac跳出 编辑:程序博客网 时间:2024/05/21 17:14
给定一个字符串,有两个操作。 1:改变某个字符。 2:判断某个区间是否构成回文串。 直接判断会超时。
方法:多项式哈希+线段树。
对于任意一个区间L,L+1,...,R
KeyL=str[ L] +str[L+1]*K + str[L+2]* K^2 +...str[R] * K^(R-L)
KeyR=str[R] +str[R-1]*K + str[R-2]* K^2 +...str[L] * K^(R-L)
只要是回文串,则KeyL 与 KeyR 会相等,K为某常数。
用线段树维护每个区间的KeyL 和 KeyR.
刚开始,K的次方是写的快速幂,超时。然后预处理存进数组就快多了。
#define FOR(i,n) for(long long (i)=1;(i)<=(n);(i)++)#define For(i,n) for(long long (i)=0;(i)<(n);(i)++)#define K 137#define maxn 100001 #define ls l,m,rt<<1#define rs m+1,r,rt<<1|1 using namespace std;char str[maxn];int Pow[maxn];int N,M; struct Node{int KeyL,KeyR;Node():KeyL(0),KeyR(0){}void init(){KeyL=KeyR=0;}}node[maxn<<2];void PushUp(int L,int R,int rt){node[rt].KeyL=node[rt<<1].KeyL+node[rt<<1|1].KeyL*Pow[L];node[rt].KeyR=node[rt<<1].KeyR*Pow[R]+node[rt<<1|1].KeyR;}void build(int l,int r,int rt){if(l==r){node[rt].KeyL=node[rt].KeyR=str[l];return;}int m=(l+r)>>1;build(ls);build(rs);PushUp(m-l+1,r-m,rt);}void update(int a,char b,int l,int r,int rt){if(l==r){node[rt].KeyL=node[rt].KeyR=b;return;}int m=(l+r)>>1;if(a<=m) update(a,b,ls);if(a> m) update(a,b,rs);PushUp(m-l+1,r-m,rt);}Node query(int L,int R,int l,int r,int rt){if(L <=l && r <=R){return node[rt];}Node ANS,ANS1,ANS2;int T=0;int m=(l+r)>>1;if(L <= m) ANS1=query(L,R,ls),T+=1;if(R >m ) ANS2=query(L,R,rs),T+=2;if(T==1){ANS=ANS1;}else if(T==2) {ANS=ANS2;}else if(T==3) {ANS.KeyL=ANS1.KeyL+ANS2.KeyL*Pow[m-max(L,l)+1];ANS.KeyR=ANS1.KeyR*Pow[min(R,r)-m]+ANS2.KeyR;}return ANS;}int main(void){while(~scanf("%s",str+1)){str[0]='0';N=strlen(str)-1;Pow[0]=1;FOR(i,N) Pow[i]=Pow[i-1]*K;For(i,maxn<<2) node[i].init();scanf("%d",&M);build(1,N,1);For(i,M){char op[20];scanf("%s",op);if(op[0]=='c'){int a;char b;scanf("%d %c",&a,&b);update(a,b,1,N,1);}else{int a,b;scanf("%d%d",&a,&b);Node ANS=query(a,b,1,N,1);bool T=ANS.KeyL==ANS.KeyR;if(T) printf("Yes\n");else printf("No\n");}} }return 0;}
0 0
- URAL 1989. Subpalindromes
- URAL 1989 Subpalindromes
- URAL 1989 Subpalindromes
- URAL 1989 Subpalindromes 思路
- Subpalindromes
- Subpalindromes
- URAL 1989 Subpalindromes(多项式哈希+线段树)
- URAL 1989 Subpalindromes(字符串HASH&线段树单点更新)
- URAL 1989 Subpalindromes(多项式哈希+线段树)
- URAL 1989 Subpalindromes(回文串 线段树 多项式hash)
- URAL 1989 Subpalindromes(线段树单点修改+字符串hash)
- bunoj 36907 Subpalindromes
- URAL
- 【ural】
- URAL
- URAL
- URAL
- URAL
- IMSdroid视频马赛克问题
- codility test 4
- 深究repo的所谓“续传”功能
- if (!ProcessShellCommand(cmdInfo))出错
- 提高系统性能之多线程编程
- URAL 1989. Subpalindromes
- 学习Spring必学的Java基础知识(3)----PropertyEditor
- 题目1199:找位置
- POJ3080 Blue Jeans
- 优秀程序员必须知道的32个算法,提高你的开发效率
- 程序员如何走出自己的安逸环境
- 题目1121:首字母大写
- 使用java代码调用exe程序 (包括参数传递)
- 很简单的爬楼梯问题