题目大意:给一行字符串,两种操作:change(pos,char),将pos处字符改为char;isPalindrome(i,j),询问[i,j]之间是否为回文字符串。

题目分析:做正反两次字符串哈希,如果哈希值一样则回文。用线段树维护哈希值,单点更新即可。

我的挫代码如下:

# include<cstdio>
# include<iostream>
# include<cstring>
# include<algorithm>
using namespace std;
# define mid (l+(r-l)/2) const int N=100000; int seed[2]={31,131};
unsigned int base[2][N+5]; char str[N+5];
char op[2];
unsigned int tr_left[2][N*4+5];
unsigned int tr_right[2][N*4+5]; struct Node{
unsigned int left[2];
unsigned int right[2];
}; inline void init()
{
for(int i=0;i<2;++i){
base[i][0]=1;
for(int j=1;j<=N;++j){
base[i][j]=base[i][j-1]*seed[i];
}
}
} inline void read(int &x)
{
x=0;
char c;
while((c=getchar())&&(c<'0'||c>'9'));
x=c-'0';
while(c=getchar()){
if(c<'0'||c>'9') break;
x=x*10+c-'0';
}
} inline bool ok(Node *a)
{
for(int i=0;i<2;++i)
if(a->left[i]!=a->right[i]) return false;
return true;
} inline void pushUp(int rt,int l,int r)
{
for(int i=0;i<2;++i){
tr_left[i][rt]=tr_left[i][rt<<1]*base[i][r-mid]+tr_left[i][rt<<1|1];
tr_right[i][rt]=tr_right[i][rt<<1|1]*base[i][mid-l+1]+tr_right[i][rt<<1];
}
} inline void build(int rt,int l,int r)
{
if(l==r){
for(int i=0;i<2;++i)
tr_left[i][rt]=tr_right[i][rt]=str[l]-'a'+1;
}else{
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
pushUp(rt,l,r);
}
} inline void update(int rt,int l,int r,int x,char c)
{
if(l==r){
for(int i=0;i<2;++i)
tr_left[i][rt]=tr_right[i][rt]=c-'a'+1;
}else{
if(x<=mid) update(rt<<1,l,mid,x,c);
else update(rt<<1|1,mid+1,r,x,c);
pushUp(rt,l,r);
}
} inline Node* query(int rt,int l,int r,int L,int R)
{
Node* nde=new Node;
if(L<=l&&r<=R){
for(int i=0;i<2;++i){
nde->left[i]=tr_left[i][rt];
nde->right[i]=tr_right[i][rt];
}
}else{
Node* nde1=NULL;
Node* nde2=NULL;
if(L<=mid) nde1=query(rt<<1,l,mid,L,min(R,mid));
if(R>mid) nde2=query(rt<<1|1,mid+1,r,max(mid+1,L),R);
if(nde1!=NULL&&nde2!=NULL){
for(int i=0;i<2;++i){
nde->left[i]=nde1->left[i]*base[i][R-mid]+nde2->left[i];
nde->right[i]=nde2->right[i]*base[i][mid-L+1]+nde1->right[i];
}
}else{
if(nde1!=NULL){
for(int i=0;i<2;++i){
nde->left[i]=nde1->left[i];
nde->right[i]=nde1->right[i];
}
}else if(nde2!=NULL){
for(int i=0;i<2;++i){
nde->left[i]=nde2->left[i];
nde->right[i]=nde2->right[i];
}
}
}
if(nde1!=NULL) delete nde1;
if(nde2!=NULL) delete nde2;
}
return nde;
} int main()
{
init();
int m;
while(~scanf("%s",str))
{
int n=strlen(str);
build(1,0,n-1);
scanf("%d",&m);
int a,b;
char ch[2];
while(m--)
{
scanf("%s",op);
if(op[0]=='p'){
read(a);
read(b);
Node *nde=query(1,0,n-1,a-1,b-1);
if(ok(nde)) printf("Yes\n");
else printf("No\n");
delete nde;
}else if(op[0]=='c'){
read(a);
scanf("%s",ch);
update(1,0,n-1,a-1,ch[0]);
}
}
}
return 0;
}

  

URAL-1989 Subpalindromes(单点更新+hash)的更多相关文章

  1. URAL 1989 Subpalindromes (多项式hash) +【线段树】

    <题目链接> <转载于 >>>  > 题目大意:给你一段字符串,进行两种操作:1.询问[l,r]这个区间中的字符串是否是回文串: 2.更改该字符串中对应下标的 ...

  2. N - Subpalindromes URAL - 1989 哈希+线段树

    N - Subpalindromes URAL - 1989 这个是一个哈希+线段树,这个题目也不算特别难,但是呢,还比较有意思. 这个题目给你两个操作,一个是回答l~r 区间是不是回文,一个是对一个 ...

  3. ural1989 单点更新+字符串hash

    正解是双哈希,不过一次哈希也能解决.. 然后某个数字就对应一个字符串,虽然有些不同串对应同一个数字,但是概率非常小,可以忽略不计.从左到右.从右到左进行两次hash,如果是回文串,那么对应的整数必定存 ...

  4. 单点更新线段树 RMQ

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

  5. ural 1989 subplindromes

    https://vjudge.net/problem/URAL-1989 题意: 先给出一个字符串,对于这个字符串,有两种操作,一种是询问从下标x到y的串是不是回文串,另一种是将下标为pos的字符改为 ...

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

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

  7. HDU 1166 敌兵布阵 线段树单点更新求和

    题目链接 中文题,线段树入门题,单点更新求和,建一棵树就可以了. #include <iostream> #include <cstdio> #include <cmat ...

  8. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  9. UVA 12299 RMQ with Shifts(线段树:单点更新)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

随机推荐

  1. 利用K2和Microsoft Dynamics CRM构建业务App的5大理由

    Microsoft Dynamics CRM提供了一个绝佳的客户关系管理平台,使您能够创建各种以客户为中心的解决方案.然而,通过将K2的企业业务流程功能与Microsoft Dynamics CRM相 ...

  2. DotNetBar v12.4.0.2 Fully Cracked

    更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.4.0.2 如果遇到破解问题可以与我 ...

  3. 计算文字的高度和宽度--以微博会话界面中用户名(userName)为例

    所用方法 // NOTE: All of the following methods will default to drawing on a baseline, limiting drawing t ...

  4. sql 解析字符串添加到临时表中 sql存储过程in 参数输入

    sql 解析字符串添加到临时表中  sql存储过程in 参数输入 解决方法 把字符串解析 添加到 临时表中 SELECT * into #临时表   FROM dbo.Func_SplitOneCol ...

  5. python版恶俗古风自动生成器.py

    python版恶俗古风自动生成器.py """ python版恶俗古风自动生成器.py 模仿自: http://www.jianshu.com/p/f893291674c ...

  6. IOS 中的KVO模式 观察者模式

    / // KvoObject.h // KVO // // Created by lin kang on 16/6/7. // Copyright © 2016年 lin kang. All righ ...

  7. 转:Java面试题集(51-70) http://blog.csdn.net/jackfrued/article/details/17403101

    Java面试题集(51-70) Java程序员面试题集(51-70) http://blog.csdn.net/jackfrued/article/details/17403101 摘要:这一部分主要 ...

  8. JS原生回到顶部效果

    // 回到顶部 onload = function () { var oBtnTop = document.getElementById('toTop'); var timer = null; oBt ...

  9. 修复win8引导

    格式化那个350MB的分区(Win8安装盘启动之后挂载在c:之后,用Win8的安装U盘,进去修复模式,然后进入高级选项的命令行提示符模式.接着,转到安装现有Win8的分区(Win8安装盘启动之后挂载在 ...

  10. git vs svn

    http://www.tuicool.com/articles/e2MnAb Git与SVN的不同之处 svn为集中化的版本控制,svn获取最新的版本或者提交更新,历史记录等信息每次都要连接中央版本库 ...