URAL-1989 Subpalindromes(单点更新+hash)
题目大意:给一行字符串,两种操作: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)的更多相关文章
- URAL 1989 Subpalindromes (多项式hash) +【线段树】
<题目链接> <转载于 >>> > 题目大意:给你一段字符串,进行两种操作:1.询问[l,r]这个区间中的字符串是否是回文串: 2.更改该字符串中对应下标的 ...
- N - Subpalindromes URAL - 1989 哈希+线段树
N - Subpalindromes URAL - 1989 这个是一个哈希+线段树,这个题目也不算特别难,但是呢,还比较有意思. 这个题目给你两个操作,一个是回答l~r 区间是不是回文,一个是对一个 ...
- ural1989 单点更新+字符串hash
正解是双哈希,不过一次哈希也能解决.. 然后某个数字就对应一个字符串,虽然有些不同串对应同一个数字,但是概率非常小,可以忽略不计.从左到右.从右到左进行两次hash,如果是回文串,那么对应的整数必定存 ...
- 单点更新线段树 RMQ
D. Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input ...
- ural 1989 subplindromes
https://vjudge.net/problem/URAL-1989 题意: 先给出一个字符串,对于这个字符串,有两种操作,一种是询问从下标x到y的串是不是回文串,另一种是将下标为pos的字符改为 ...
- HDU 1754 I Hate It 线段树单点更新求最大值
题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...
- HDU 1166 敌兵布阵 线段树单点更新求和
题目链接 中文题,线段树入门题,单点更新求和,建一棵树就可以了. #include <iostream> #include <cstdio> #include <cmat ...
- HDU 1166 敌兵布阵(线段树单点更新)
敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...
- UVA 12299 RMQ with Shifts(线段树:单点更新)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
随机推荐
- android webview实战
webSettings = wvShowProduce.getSettings();//设置WebView属性,能够执行Javascript脚本webSettings.setJavaScriptEna ...
- [转]C++运算优先级列表
From:http://en.cppreference.com/w/cpp/language/operator_precedence Precedence Operator Description A ...
- Android获取手机设备识别码(IMEI)和手机号码
最近看了下获取手机设备ID和手机信息以及SIM的信息例子,主要还是借鉴别人的,现在自己写一下,算是巩固加深了,也希望能给大家一个参考 必要的条件还是一部真机,SIM卡或者UIM卡. 首先,在Andro ...
- Python File I/O
File is a named location on disk to store related information. It is used to permanently store data ...
- java基础-003
10.进程和线程 进程是执行者的应用程序,而线程是进程内部的一个执行序列.一个进程可以有多个线程.线程又叫轻量级进程. 创建线程的三种方式: I> 继承Thread类 II> 实现Runn ...
- fdisk分区
查看文件系统: # df -hFilesystem Size Used Avail Use% Mounted on/dev/sda1 20G 1.1G 18G 6% /tmpfs 1.9G 0 1.9 ...
- ODI中显示us7ascii字符集的测试
安装oracle DB时,选择的字符集:美国.英语.US7ASCII. 在不设置nls_lang的情况,插入中文,成功,但存进去的是乱码,select看到也是??(无论后面再怎么设置nls_lang) ...
- history对象
1.history对象前进 history.forward() 2.history对象后退 history.back() 3.history对象跳入指定页面 history.go(-1): //当前 ...
- poj3126 筛素数+bfs
//Accepted 212 KB 16 ms //筛素数+bfs #include <cstdio> #include <cstring> #include <iost ...
- js面向对象(构造函数与继承)
深入解读JavaScript面向对象编程实践 Mar 9, 2016 面向对象编程是用抽象方式创建基于现实世界模型的一种编程模式,主要包括模块化.多态.和封装几种技术. 对JavaScript而言,其 ...