正解是双哈希,不过一次哈希也能解决。。

然后某个数字就对应一个字符串,虽然有些不同串对应同一个数字,但是概率非常小,可以忽略不计。从左到右、从右到左进行两次hash,如果是回文串,那么对应的整数必定存在某种关系(可以理解成相等),对于更新操作,就是单点更新。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#include<algorithm>
#define ll unsigned long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 100005
char s[maxn];
ll F[maxn+];
int len;
struct node{
ll suml,sumr;
}tree[maxn<<];//区间保存从左到右和从右到左的hash值
inline void pushup(int rt){
tree[rt].suml=tree[rt<<].suml+tree[rt<<|].suml;
tree[rt].sumr=tree[rt<<].sumr+tree[rt<<|].sumr;
}
void build(int l,int r,int rt){
if(l==r){
tree[rt].suml=F[l-]*(s[l-]-'a');
tree[rt].sumr=F[len-l]*(s[l-]-'a');
return;
}
int m=l+r>>;
build(lson);
build(rson);
pushup(rt);
}
void update(int pos,int val,int l,int r,int rt){//单点更新
if(l==r){
tree[rt].suml=F[l-]*val;
tree[rt].sumr=F[len-l]*val;
return;
}
int m=l+r>>;
if(pos<=m) update(pos,val,lson);
else update(pos,val,rson);
pushup(rt);
}
ll suml,sumr;
void query(int x,int y,int l,int r,int rt){
if(x<=l && y>=r){
suml+=tree[rt].suml;
sumr+=tree[rt].sumr;
return;
}
int m=l+r>>;
if(x<=m) query(x,y,lson);
if(y>m) query(x,y,rson);
}
int main(){
F[]=;
for(int i=;i<=maxn;i++)
F[i]=F[i-]*;//打表处理
int q;
while(scanf("%s",s)!=EOF){
scanf("%d",&q);
len=strlen(s);
build(,len,);
char op[];
int x,y;
while(q--){
scanf("%s",op);
if(op[]=='p'){//查询
scanf("%d%d",&x,&y);
suml=sumr=;
query(x,y,,len,);
int k1=x-;//左到右的长度
int k2=len-y;//右到左的区间长度
if(k1>k2) sumr*=F[k1-k2];
else suml*=F[k2-k1];
cout<<suml<<" "<<sumr<<endl;
if(suml==sumr) puts("YES");
else puts("NO");
}
else {//单点修改
int x;
char tmp[];
scanf("%d%s",&x,tmp);
update(x,tmp[]-'a',,len,);
}
}
}
return ;
}

ural1989 单点更新+字符串hash的更多相关文章

  1. URAL-1989 Subpalindromes(单点更新+hash)

    题目大意:给一行字符串,两种操作:change(pos,char),将pos处字符改为char:isPalindrome(i,j),询问[i,j]之间是否为回文字符串. 题目分析:做正反两次字符串哈希 ...

  2. 单点更新线段树 RMQ

    D. Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input ...

  3. HDU 1880 魔咒词典 (字符串hash)

    <题目链接> 题目大意: 就是每个字符串有一个配套的对应字符串,询问的时候,无论输出其中的哪一个字符串,输出另一个,如果不存在这个字符串,直接输出"what?". 解题 ...

  4. 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch

    Kefa and Watch Problem's Link Mean: 给你一个长度为n的字符串s,有两种操作: 1 L R C : 把s[l,r]全部变为c; 2 L R d : 询问s[l,r]是 ...

  5. [CQOI2014][bzoj3507] 通配符匹配 [字符串hash+dp]

    题面 传送门 思路 0x01 KMP 一个非常显然而优秀的想法:把模板串按照'*'分段,然后对于每一段求$next$,'?'就当成可以对于任意字符匹配就行了 对于每个文本串,从前往后找第一个可以匹配的 ...

  6. NYOJ-568/1012//UVA-12299RMQ with Shifts,线段树单点更新+区间查询

    RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 ->  Link1  <- -> Link2  <- 以上两题题意是一样 ...

  7. Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)

    Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...

  8. CF1200E Compress Words | 字符串hash

    传送门 Examples input 1 5 I want to order pizza output 1 Iwantorderpizza input 2 5 sample please ease i ...

  9. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

随机推荐

  1. how to calculate the best fit to a plane in 3D, and how to find the corresponding statistical parameters

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  2. P4315 月下“毛景树”

    P4315 月下"毛景树" 题目描述 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里. 爬啊爬~爬啊爬 ...

  3. VBScript常用函数

    VBScript常用函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1.Abs函数 Option Explicit '@author :yinzhengjie 'blog:ht ...

  4. Nginx的alias的用法及与root的区别

    以前只知道Nginx的location块中的root用法,用起来总是感觉满足不了自己的一些想法.然后终于发现了alias这个东西. 先看toot的用法 location /request_path/i ...

  5. Hadoop记录-Hadoop shell常用命令

  6. Docker 入门 第六部分:部署app

    目录 Docker 入门 第六部分:部署app 先决条件 介绍 选择一个选项 Docker CE(Cloud provider) Enterprise(Cloud provider)这里不做介绍 En ...

  7. Selenium自动化Page模式(Python)

    Selenium是当前主流的web自动化工具,提供了多种浏览器的支持(Chrome,Firefox, IE等等),当然大家也可以用自己喜欢的语言(Java,C#,Python等)来写用例,很容易上手. ...

  8. wcf事务

    wcf服务 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serial ...

  9. 发现一sonar-runner bug

    最近在使用sonar-runner做代码扫描, 在windows环境运行多模块的扫描ok,但是在linux上sonar-runner扫描多模块报错: 先贴sonar-project.propertie ...

  10. Django 2.0 学习(12):Django 模板语法

    Django 模板语法 一.模板 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法 模板语法变量:{{ }} 在Django模板中遍历复杂数据结构的关键是句点字 ...