bzoj 1014 splay
首先我们可以用splay来维护这个字符串,那么对于某两个位置的lcp,维护每个节点的子树的hash,然后二分判断就好了。
/**************************************************************
Problem: 1014
User: BLADEVIL
Language: C++
Result: Accepted
Time:4468 ms
Memory:3640 kb
****************************************************************/
//By BLADEVIL
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100010
using namespace std;
char s[maxn];
int fac[maxn],key[maxn],num,rot,son[maxn][],father[maxn],size[maxn],hash[maxn];
void update(int x) {
if (!x) return ;
hash[x]=hash[son[x][]]+(key[x]+hash[son[x][]]*)*fac[size[son[x][]]];
size[x]=size[son[x][]]+size[son[x][]]+;
}
int build(int l,int r) {
int mid=l+r>>,left=,right=;
if (mid<r) right=build(mid+,r);
if (mid>l) left=build(l,mid-);
father[left]=father[right]=mid;
son[mid][]=left; son[mid][]=right;
update(mid);
return mid;
}
void rotate(int x,int &rot) {
int y=father[x],z=father[y];
int p=(son[y][]==x),q=p^;
if (y==rot) rot=x; else if (son[z][]==y) son[z][]=x; else son[z][]=x;
father[x]=z; father[y]=x; father[son[x][q]]=y;
son[y][p]=son[x][q]; son[x][q]=y;
update(y);
}
void splay(int x,int &rot) {
while (x!=rot) {
int y=father[x],z=father[y];
if (y!=rot)
if ((son[y][]==x)^(son[z][]==y)) rotate(x,rot); else rotate(y,rot);
rotate(x,rot);
}
update(x);
}
int find(int x) {
int t=rot;
while () {
if (size[son[t][]]+==x) return t; else
if (size[son[t][]]+>x) t=son[t][]; else
if (size[son[t][]]+<x) x-=size[son[t][]]+,t=son[t][];
}
}
bool judge(int x,int y,int len) {
if (len==) return key[find(x+)]==key[find(y+)];
int p=find(x),q=find(x+len+);
splay(p,rot); splay(q,son[rot][]);
int a1=hash[son[q][]];
p=find(y); q=find(y+len+);
splay(p,rot); splay(q,son[rot][]);
int a2=hash[son[q][]];
return a1==a2;
}
int main() {
scanf("%s",&s); num=strlen(s);
fac[]=; for (int i=;i<maxn;i++) fac[i]=fac[i-]*;
for (int i=;i<=num+;i++) key[i]=s[i-]-'a'+; num+=;
rot=build(,num);
int task; scanf("%d",&task);
while (task--) {
int x,y;
scanf("%s",&s);
if (s[]=='Q') {
scanf("%d%d",&x,&y);
if (x>y) swap(x,y);
int l=,r=num-y-,mid,ans=;
while (l<=r) {
mid=l+r>>;
if (judge(x,y,mid)) ans=mid, l=mid+; else r=mid-;
}
printf("%d\n",ans);
} else
if (s[]=='R') {
scanf("%d%s",&x,&s);
int p=find(x+);
key[p]=s[]-'a'+;
splay(p,rot);
} else
if (s[]=='I') {
scanf("%d%s",&x,&s);
key[++num]=s[]-'a'+;
int p=find(x+); splay(p,rot);
int q=find(x+); splay(q,son[rot][]);
father[num]=q; son[q][]=num;
splay(num,rot);
}
}
return ;
}
bzoj 1014 splay的更多相关文章
- bzoj 1014 splay维护hash值
被后缀三人组虐了一下午,写道水题愉悦身心. 题很裸,求lcq时二分下答案就行了,写的不优美会被卡时. (写题时精神恍惚,不知不觉写了快两百行...竟然调都没调就A了...我还是继续看后缀自动机吧... ...
- [BZOJ 1014] [JSOI2008] 火星人prefix 【Splay + Hash】
题目链接:BZOJ - 1014 题目分析 求两个串的 LCP ,一种常见的方法就是 二分+Hash,对于一个二分的长度 l,如果两个串的长度为 l 的前缀的Hash相等,就认为他们相等. 这里有修改 ...
- BZOJ 1014 火星人 | 平衡树维护哈希
BZOJ 1014 火星人 题意 有一个字符串,三中操作:在某位置后面插入一个字符.修改某位置的字符.询问两个后缀的最长公共前缀. 题解 看到网上的dalao们都说这道题是平衡树,我就很懵x--平衡树 ...
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6243 Solved: 2007[Submit] ...
- BZOJ 1014: [JSOI2008]火星人prefix Splay+二分
1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...
- bzoj 1014 [JSOI2008]火星人prefix(splay+hash)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1014 [题意] 给定一个字符串,要求提供修改一个字符,插入一个字符,查询两个后缀LCP ...
- bzoj 1014: [JSOI2008]火星人prefix hash && splay
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3154 Solved: 948[Submit][ ...
- BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )
用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...
- BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 8112 Solved: 2569[Submit] ...
随机推荐
- MVC4 DropDownList (二) — 省市联动
1.添加省份和城市类 //省份 public class Province { public int Id { get; set; } public string Name { get; set; } ...
- 3ds Max学习日记(八)
再来更新一波学习进度. 之前玩了一下3dsmax里的灯光,不过由于和教程里的版本不同,教程里的我的没有,我有的教程又没有,所以只能瞎jb玩一玩. 最近又想建个人物模型玩玩,于是上网搜一下有 ...
- MyBatis原理系列
原理分析之一:从JDBC到Mybatis 原理分析之二:框架整体设计 原理分析之三:初始化(配置文件读取和解析) 原理分析之四:一次SQL查询的源码分析
- veeValidate
网站 http://vee-validate.logaretm.com/index.html#about 自定义为空时候的提示 Field-specific Custom Messages You m ...
- AC自动机裸题
HDU 2222 Keywords Search 模板题.对模式串建立AC自动机然后在trie树上找一遍目标串即可. # include <cstdio> # include <cs ...
- BZOJ 1211 树的计数(purfer序列)
首先考虑无解的情况, 根据purfer序列,当dee[i]=0并且n!=1的时候,必然无解.否则为1. 且sum(dee[i]-1)!=n-2也必然无解. 剩下的使用排列组合即可推出公式.需要注意的是 ...
- Python 基本数据结构
Python基本数据结构 数据结构:通俗点儿说,就是存储数据的容器.这里主要介绍Python的4种基本数据结构:列表.元组.字典.集合: 格式如下: 列表:list = [val1, val2, va ...
- Go语言【第八篇】:Go语言变量作用域
Go语言变量作用域 作用域为已声明标识符所表示的常量.类型.变量.函数或包在源代码中的作用范围,Go语言中变量可以在三个地方声明: 函数内定义的变量称为局部变量: 函数外定义的变量称为全局变量: 函数 ...
- sqlserver修改sa密码(在windows登陆没有权限的情况下)
对于windows用户没有权限执行alter login sa enable的情况下,采用如下方法可以成功修改sa密码登陆. . 用Run as a administrator打开命令提示符里输入NE ...
- 【以前的空间】bzoj 1072 [SCOI2007]排列perm
又颓废了一个下午,最近撸mc撸到丧失意识了,玩的有点恶心,于是找水题做,瞧不起颓废的自己啊. another水题. 这题题意很明显啦,就是找数字排列后组成的数去mod d=0后有多少种. 普通的搜索的 ...