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 ...
随机推荐
- 动态规划:最长上升子序列(LIS)
转载请注明原文地址:http://www.cnblogs.com/GodA/p/5180560.html 学习动态规划问题(DP问题)中,其中有一个知识点叫最长上升子序列(longest incre ...
- Redis_php 学习
转载内容: PhpRedis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系很有用;以下是redis官方提供的命令使用技巧: Redis::__construct构造函数 1 ...
- hdoj 2553 N皇后问题【回溯+打表】
N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- windows进程间通信 .
摘 要: 随着人们对应用程序的要求越来越高,单进程应用在许多场合已不能满足人们的要求.编写多进程/多线程程序成为现代程序设计的一个重要特点,在多进程程序设计中,进程间的通信是不可避免的.Microso ...
- [一]java环境变量的配置
1.JAVA_HOME(新建):D:\jdk1.6 2.classpath(新建): .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar; 3.path(新增):% ...
- Android Fragment之间传值
首先来介绍的是我们的默认的布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- 微信中QQ表情的解析(php)
微信公众平台接受的消息中,标签是用'/:'开头的字符串表示的,假设要在网页上显示(比方制作微信大屏幕),就须要进行转换. 所以我向微信公众平台按顺序发送了各个QQ表情,在微信公众平台后台能够看到接受的 ...
- [React] React Fundamentals: State Basics
State is used for properties on a component that will change, versus static properties that are pass ...
- MantisBT
官方网站:http://www.mantisbt.org/ MantisBT is a free popular web-based bugtracking system (feature list) ...
- Winedt10 添加自定义宏
Winedt10 添加自定义功能,并在toolbar上添加快捷命令 功能描述: 用宏实现latex+bib参考文献的一键编译. Remark: The toolbar is the most visi ...