思路:插入、修改操作是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的更多相关文章

  1. [BZOJ1014][JSOI2008]火星人prefix

    [BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...

  2. BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*

    BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...

  3. 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)

    [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...

  4. bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash 带修改带插入:splay维护 #include< ...

  5. [BZOJ1014] [JSOI2008] 火星人prefix (splay & 二分答案)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  6. bzoj1014: [JSOI2008]火星人prefix splay+hash+二分

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  7. [bzoj1014](JSOI2008)火星人 prefix (Splay维护哈希)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀. 比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 ...

  8. BZOJ1014[JSOI2008]火星人prefix(splay维护hash)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  9. BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)

    题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...

  10. bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)

    题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...

随机推荐

  1. HW5.13

    public class Solution { public static void main(String[] args) { System.out.printf("%s\t%s\n&qu ...

  2. 阿里云服务器(CentOS)安装tomcat,jdk,布署J2EE项目

    1.使用Xshell登录服务器,当然你也可以使用其他软件登录服务器 2.Linux服务器挂载数据盘,具体参见视频教程(quote:"一般来说服务器的数据盘需要和系统盘分开,当系统出现故障后能 ...

  3. (一)Redis初学教程之安装篇

    1.下载windows下Redis服务安装程序(有32位的和64位的,识操作系统安装) 下载地址:https://github.com/dmajkic/redis/downloads 2.安装教程(详 ...

  4. zabbix邮件报警脚本

    #!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_ ...

  5. HDU 4849-Wow! Such City!(最短路)

    Wow! Such City! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Other ...

  6. mfc开发问题_v1

    1. 设置对话框按钮背景图片? 首先,设置对话框按钮的属性为Bitmap,然后导入资源文件(一个你需要作为背景的小图片),最后在该对话框类的OnInitDialog函数中添加如下代码: //设置对话框 ...

  7. oracle学习----trace文件

    1.查看v$diag_info视图,能查看跟踪文件的位置,具体的名称等信息. name列中,Diag Trace是11g跟踪文件所在位置 Default Trace File是当前会话的跟踪文件名 2 ...

  8. javascript变量提升

    提升是一种将变量和函数的声明移到函数作用域(如果不在任何函数内的话就是全局作用域)最顶部的机制. 提升影响了变量的生命周期,一个变量的生命周期包含3个阶段: 声明——创建一个新变量,例如var myV ...

  9. Hibernate事务管理

    User类: public class User implements Serializable{ public User(){} private Integer id; private String ...

  10. Java并发——Fork/Join框架

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/4631466. ...