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] ...
随机推荐
- 【Linux】- 文件基本属性
Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限.为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规定. 在Linux中我们可 ...
- (转)linux IO 内核参数调优 之 参数调节和场景分析
1. pdflush刷新脏数据条件 (linux IO 内核参数调优 之 原理和参数介绍)上一章节讲述了IO内核调优介个重要参数参数. 总结可知cached中的脏数据满足如下几个条件中一个或者多个的时 ...
- Flink的序列化与flink-hadoop-compatibility
最近 用户提交了一个问题 说他的jar包里明明包含相关的类型 但是在提交Flink作业的时候 却报出classnotfound的错误 查看之后发现 这里是flink的一个没有说的太明白的地方 用户的代 ...
- 新浪云部署java web程序 注意事项
在新浪云新手指南里有部署java的示例,但是对一个新手来说难免会有一些地方犯错,折腾了好长时间才把自己的java web部署到了新浪云.这里主要写一些我遇到的问题与第一次使用新浪云的朋友分享一下. 首 ...
- python的if语句、while循环、for循环
if语句 计算机又被称作电脑,意指计算机可以像人脑一样,根据周围环境条件(即expession)的变化做出不同的反应(即执行代码)if语句就是来控制计算机实现这一功能 语法: 1.单分支,单个条件判断 ...
- POJ3415:Common Substrings——题解
http://poj.org/problem?id=3415 给定两个字符串A 和B,求长度不小于k 的公共子串的个数(可以相同). 论文题,和上道题(POJ2774)类似,首先想到现将AB串合并,然 ...
- BZOJ3894:文理分科——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3894 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理 ...
- CodeForces.71A Way Too Long Words (水模拟)
CodeForces71A. Way Too Long Words (水模拟) 题意分析 题怎么说你怎么做 没有坑点 代码总览 #include <iostream> #include & ...
- 51nod 1257 背包问题 V3(分数规划)
显然是分数规划...主要是不会求分数的形式,看了题解发现自己好傻逼QAQ 还是二分L值算出d[]降序选K个,顺便记录选择时候的p之和与w之和就可以输出分数形式了... #include<iost ...
- jquery 操作实例一
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...