bzoj1014:[JSOI2008]火星人prefix
思路:插入、修改操作是splay的模型,然后询问的话就可以二分答案,然后再用splay去判,关键就在于怎么去判断。
可以用字符串hash,splay每个节点维护一个hash域,然后就可以定义一个进制去hash即可二分判断,hash值让其自然溢出即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 100005
#define p 37 int n,m;
unsigned int power[maxn];
char s[maxn],t[5]; struct splay_tree{
int ch[maxn][2],fa[maxn],note[maxn],size[maxn],root,tot;
unsigned int hash[maxn],val[maxn];
void update(int x){
if (!x) return;int ls=ch[x][0],rs=ch[x][1];
size[x]=size[ls]+size[rs]+1;
hash[x]=hash[ls]*power[size[rs]+1]+val[x]*power[size[rs]]+hash[rs];
}
void newnode(int &x,int v,int bo){val[x=++tot]=v,note[x]=bo;}
void build(int &x,int l,int r,int bo){
if (l>r) return;int mid=(l+r)>>1;
newnode(x,(mid!=0&&mid!=n+1)?s[mid]-'a':0,bo);
build(ch[x][0],l,mid-1,0),build(ch[x][1],mid+1,r,1);
fa[ch[x][0]]=fa[ch[x][1]]=x;
update(x);
}
void rotate(int x){
int y=fa[x],z=fa[y],bo=note[x],bo1=note[y];
ch[y][bo]=ch[x][bo^1],fa[ch[x][bo^1]]=y;
ch[x][bo^1]=y,fa[y]=x;
fa[x]=z;if (bo1!=2) ch[z][bo1]=x;
note[x]=bo1,note[y]=bo^1,note[ch[y][bo]]=bo;
update(y);
}
void splay(int x){
while (note[x]!=2){
if (note[x]==note[fa[x]]) rotate(fa[x]);
rotate(x);
}
root=x,update(x);
}
int find(int pos){
int x=root;
while (1){
if (size[ch[x][0]]+1==pos) return splay(x),x;
else if (size[ch[x][0]]>=pos) x=ch[x][0];
else pos-=size[ch[x][0]]+1,x=ch[x][1];
}
}
int suc(int x){x=ch[x][1];while (ch[x][0]) x=ch[x][0];return x;}
void insert(int x,int v){
splay(x);int y=suc(x),z;newnode(z,v,0);
fa[ch[x][1]]=0,note[ch[x][1]]=2,splay(y);
fa[y]=x,note[y]=1,ch[x][1]=y;
fa[z]=y,ch[y][0]=z,splay(z);
}
void change(int x,int v){val[x]=v;splay(x);}
unsigned int gethash(int l,int len){
int r=l+len-1,x=find(l),y=find(r+2);
splay(x),fa[ch[x][1]]=0,note[ch[x][1]]=2,splay(y);
note[y]=1,fa[y]=x,ch[x][1]=y;
unsigned int ans=hash[ch[y][0]];
return splay(y),ans;
}
bool check(int x,int y,int len){return gethash(x,len)==gethash(y,len);}
}S; void solve(int x,int y){
int l=1,r=min(S.tot-x-1,S.tot-y-1),ans=0;
while (l<=r){
int mid=(l+r)>>1;
if (S.check(x,y,mid)) l=mid+1,ans=mid;
else r=mid-1;
}
printf("%d\n",ans);
} int main(){
scanf("%s",s+1),scanf("%d",&m);power[0]=1,n=strlen(s+1);
for (int i=1;i<=100001;i++) power[i]=power[i-1]*p;
S.build(S.root,0,n+1,2);
while (m--){
int x,y;scanf("%s",t+1);
if (t[1]=='I') scanf("%d%s",&x,t+1),S.insert(S.find(x+1),t[1]-'a');
else if (t[1]=='R') scanf("%d%s",&x,t+1),S.change(S.find(x+1),t[1]-'a');
else scanf("%d%d",&x,&y),solve(x,y);
}
return 0;
}
bzoj1014:[JSOI2008]火星人prefix的更多相关文章
- [BZOJ1014][JSOI2008]火星人prefix
[BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...
- BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*
BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...
- 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)
[JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...
- bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix
http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash 带修改带插入:splay维护 #include< ...
- [BZOJ1014] [JSOI2008] 火星人prefix (splay & 二分答案)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- bzoj1014: [JSOI2008]火星人prefix splay+hash+二分
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- [bzoj1014](JSOI2008)火星人 prefix (Splay维护哈希)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀. 比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 ...
- BZOJ1014[JSOI2008]火星人prefix(splay维护hash)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)
题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...
- bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)
题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...
随机推荐
- java 关于extends 和implement的区别
在java中extends用于继承父类,只要父类不是声明为final或者为abstract类就可以,但是java不支持多重继承.可以使用接口实现多重继承implements,继承只能继承一个类,但im ...
- HW2.8
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 恢复oracle中误删除drop掉的表
查看回收站中表 select object_name,original_name,partition_name,type,ts_name,createtime,droptime from recycl ...
- tcpclient 类
1. 构造函数 1) 类对象将套接字与本地系统地址和一个随机的tcp端口号进行绑定. 在默认的tcpclient 对象创建后,必须使用connect方法与远程设备连接. TcpClient tc = ...
- PC-大概最全的黑客工具表了
纯真IP数据库查询程序 remote administrator (rar解密)ZiperelloV2.0 汉化版.zip (代理跳板)SkSockServer1.09.zip [ar ...
- JSON基本操作
import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.J ...
- 理解SQL Server中索引的概念
T-SQL查询进阶--理解SQL Server中索引的概念,原理以及其他 简介 在SQL Server中,索引是一种增强式的存在,这意味着,即使没有索引,SQL Server仍然可以实现应有的功能 ...
- Thinkpad SL400安装黑苹果10.8.4全纪录
提要 还在为学习苹果开发的装备发愁么 ,也许这篇文章会给你带来一些启发. 关于黑苹果:从苹果采用intel的处理器之后,mac os被黑客破解之后可以安装在PC上,从而出现了一大批未购买苹果机而使用苹 ...
- 深入理解Linux操作系统守护进程的意义
Linux服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程(daemons)来执行的.守护进程 ...
- 匹配url - 正则表达式
/^\/(.+)\/$/g.exec(req.url)