【BZOJ1014】【JSOI2008】火星人prefix
题意:
Description
Input
1、询问。语法:Qxy,x,y均为正整数。功能:计算LCQ(x,y)限制:1<=x,y<=当前字符串长度。
2、修改。语法:Rxd,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字符串长度。
3、插入:语法:Ixd,x是非负整数,d是字符。功能:在字符串第x个字符之后插入字符d,如果x=0,则在字符串开头插入。限制:x不超过当前字符串长度
Output
对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。
题解:
这题本质是简单题。。。我没做真的亏爆。。。
没有修改的最长公共前缀有很经典的二分+哈希的$O(nlogn)$做法,相信大家都会。
考虑如何把这个做法变成可以修改的:
Splay,嗯,没了。
随便维护一下hash值即可,查询依然二分+哈希
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define inf 2147483647
#define eps 1e-9
#define ha 19260817
using namespace std;
typedef long long ll;
struct node{
int son[],siz,v,fa;
}t[];
int n,m,x,y,rt,tot,H[],num[];
char s[],op[],ch[];
int lr(int u){
return t[t[u].fa].son[]==u;
}
void updata(int u){
t[u].siz=t[t[u].son[]].siz+t[t[u].son[]].siz+;
t[u].v=(t[t[u].son[]].v+(ll)num[u]*H[t[t[u].son[]].siz]%ha+(ll)t[t[u].son[]].v*H[t[t[u].son[]].siz+]%ha)%ha;
}
void rotate(int u){
int f=t[u].fa,ff=t[f].fa,ch=lr(u);
if(ff)t[ff].son[lr(f)]=u;
t[f].son[ch]=t[u].son[ch^];
t[t[f].son[ch]].fa=f;
t[u].son[ch^]=f;
t[f].fa=u;
t[u].fa=ff;
updata(f);
updata(u);
}
void splay(int u,int g){
for(;t[u].fa!=g;rotate(u)){
int f=t[u].fa;
if(t[f].fa!=g)rotate(lr(u)==lr(f)?f:u);
}
if(!g)rt=u;
}
int findx(int u,int x){
if(!u)return ;
if(x==t[t[u].son[]].siz+)return u;
else if(x<t[t[u].son[]].siz+)return findx(t[u].son[],x);
else return findx(t[u].son[],x-t[t[u].son[]].siz-);
}
int build(int l,int r,int f){
if(l>r)return ;
int u=(l+r)/;
t[u].v=num[u]=s[u]-'';
t[u].siz=;
t[u].fa=f;
t[u].son[]=build(l,u-,u);
t[u].son[]=build(u+,r,u);
updata(u);
return u;
}
void ins(int u,int ch){
int x=findx(rt,u);
splay(x,);
int y=findx(rt,u+);
splay(y,x);
int v=++tot;
t[v].v=num[v]=ch;
t[v].siz=;
t[y].son[]=v;
t[v].fa=y;
updata(y);
updata(x);
}
void chg(int u,int ch){
int x=findx(rt,u);
splay(x,);
num[x]=ch;
updata(x);
}
int query(int l,int r){
int x=findx(rt,l-);
splay(x,);
int y=findx(rt,r+);
splay(y,x);
return t[t[y].son[]].v;
}
int work(int x,int y){
int l=,r=min(tot-x,tot-y);
while(l<r){
int mid=(l+r)/;
if(query(x+,x+mid)==query(y+,y+mid))l=mid+;
else r=mid;
}
return l-;
}
int main(){
H[]=;
for(int i=;i<=;i++)H[i]=H[i-]*%ha;
scanf("%s%d",s+,&m);
n=strlen(s+);
tot=n+;
rt=build(,n+,);
for(int i=;i<=m;i++){
scanf("%s",op);
if(op[]=='Q'){
scanf("%d%d",&x,&y);
printf("%d\n",work(x,y));
}else if(op[]=='R'){
scanf("%d%s",&x,ch);
chg(x+,ch[]-'');
}else{
scanf("%d%s",&x,ch);
ins(x+,ch[]-'');
}
}
return ;
}
【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 ...
随机推荐
- 记一次mybatis<if>标签的问题
前言 到底还是没理解清楚的锅~~~~搞了好久...啊啊啊啊 错误: There is no getter for property named 'name' in 'class java.lang.L ...
- vue 移动端项目,动态控制div距离底部的距离
<template> <div class="details"> <com-nav-bar title="保险详情"> &l ...
- CentOS7上安装google谷歌浏览器
1.首先进入根目录,然后进入etc/yum.repos.d目录下,创建google-chrome.repo文件 cd / cd etc/yum.repos.d vim google-chrome. ...
- 再次理解JS的prototype,__proto__和constructor
个人总结: 下面这篇文章很好的讲解了js原型,原型链,个人的总结是要记住这三个属性 prototype.__proto__和constructor 首先明确,js中一切都是对象object(A). ( ...
- 手写一个promise
Promise A+ 规范:https://promisesaplus.com/ 注:以下代码没有通过 promises-aplus-tests 的全部测试,但基本功能还是全的( 测试结果: 864 ...
- pip常见用法汇总
1.pip安装 yum -y install epel-release && yum -y install python-pip 2.pip安装软件 (1)安装单个软件:pip ins ...
- Ansible学习记录四:单命令测试
0.Ansible的group支持all.通配符(*).IP地址 1.查看Ansible的版本 $ ansbile --version [root@test ~]# ansible --versi ...
- centos7最小化安装Oracle11gR2
1.准备CentOS 7 系统环境 我以 CentOS-7-x86_64-DVD-1511.iso 为例,简述Oracle 11g的安装过程. 由于是使用静默模式(silent)安装的,无需使用图形化 ...
- 通过wget下载jdk
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...
- 简述Vue的响应式原理
当一个Vue实例创建时,vue会遍历data选项的属性,用 Object.defineProperty 将它们转为getter/setter并且在内部追踪相关依赖,在属性被访问和修改时通知变化.每个组 ...