题目描述:

现在,火星人定义了一个函数 LCQ(x, y)LCQ(x,y),表示:该字符串中第 xx 个字符开始的字串,与该字符串中第 yy 个字符开始的字串,两个字串的公共前缀的长度。比方说,LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0LCQ(1,7)=5,LCQ(2,10)=1,LCQ(4,7)=0
在研究 LCQLCQ 函数的过程中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出 LCQLCQ 函数的值;同样,如果求出了 LCQLCQ 函数的值,也可以很快地将该字符串的后缀排好序。

尽管火星人聪明地找到了求取 LCQLCQ 函数的快速算法,但不甘心认输的地球人又给火星人出了个难题:在求取 LCQLCQ 函数的同时,还可以改变字符串本身。具体地说,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此复杂的问题中,火星人是否还能够做到很快地求取 LCQLCQ 函数的值。

题解:

一道哈希的合并+平衡树的问题。
我们知道一般来说,$hash[i]=hash[i-1]\times k+str[i]$,$k$ 为 26 或自己定的一个素数/进制
那么考虑我们知到$[1,a]$ 的哈希值和 $[a+2,b]$ 的哈希值,而且中间字母已知,我们怎样实现合并呢?
根据对哈希的定义,$hash[1,b]=hash[1,a]+str[a+1]\times p[a]+hash[a+2,b]\times p[a+1]$
我们就能够在 $O(1)$ 的时间复杂度将 2 个区间的哈希值进行合并。
剩下的事情就交给 $Splay$ 来维护区间信息了。

Code:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
const int maxm=3000000;
const int maxn=3000000;
using namespace std;
void setIO(string a){
freopen((a+".in").c_str(),"r",stdin);
}
void debug(){
printf("ok\n");
}
char str[maxn];
unsigned int hash[maxm],p[maxm];
int n,root;
struct Splay_Tree{
int ch[maxn][2],cnt_Tree,f[maxn],key[maxn],siz[maxn];
#define lson ch[o][0]
#define rson ch[o][1]
int newnode(){return ++cnt_Tree;}
int get(int x){return ch[f[x]][1]==x;}
void pushup(int o){
if(!o) return ;
siz[o]=siz[lson]+siz[rson]+1;
hash[o]=hash[lson]+key[o]*p[siz[lson]]+hash[rson]*p[siz[lson]+1];
}
void buildSplay(int l,int r,int fa,int &o){
if(l>r) return;
int mid=(l+r)>>1;
o=newnode();
hash[o]=key[o]=str[mid]-'a';
f[o]=fa;
buildSplay(l,mid-1,o,ch[o][0]);
buildSplay(mid+1,r,o,ch[o][1]);
pushup(o);
}
void rotate(int x){
int old=f[x],oldf=f[old],which=get(x);
ch[old][which]=ch[x][which^1],f[ch[old][which]]=old;
ch[x][which^1]=old,f[old]=x;
f[x]=oldf;
if(oldf)
ch[oldf][ch[oldf][1]==old]=x;
pushup(old),pushup(x);
}
void splay(int x,int &tar){
int a=f[tar];
for(int fa;(fa=f[x])!=a;rotate(x))
if(f[fa]!=a) rotate(get(fa)==get(x)?fa:x);
tar=x;
}
int findx(int pos,int o){ //accurate position
if(pos<=siz[ch[o][0]]) return findx(pos,ch[o][0]);
else if(pos==siz[lson]+1) return o;
else return findx(pos-siz[ch[o][0]]-1,ch[o][1]);
}
void insert(int pos,int val){
int x=findx(pos,root);
splay(x,root);
int y=findx(pos+1,root);
splay(y,ch[root][1]);
int z=ch[ch[root][1]][0]=newnode(); //between
f[z]=ch[root][1];
hash[z]=key[z]=val;
pushup(z),pushup(ch[root][1]);
splay(z,root);
}
void update(int pos,int val){
splay(findx(pos,root),root);
key[root]=hash[root]=val;
pushup(root);
}
unsigned int query(int pos,int length){
int x=findx(pos-1,root); //origin
splay(x,root);
int y=findx(length+1,ch[root][1]);
splay(y,ch[root][1]); //error!!!!
int tmp=ch[root][1];
return hash[ch[tmp][0]];
}
}T;
void init(){
scanf("%s",str+2);
n=strlen(str+2);
str[n+2]='a';
str[1]='a';
n+=2;
p[0]=1;
for(int i=1;i<maxn;++i) p[i]=p[i-1]*27;
}
int main(){
//setIO("input");
init();
T.buildSplay(1,n,0,root);
int m;
scanf("%d",&m);
while(m--){
char opt[10];
scanf("%s",opt);
if(opt[0]=='Q'){
int a,b;
scanf("%d%d",&a,&b);
if(a>b) swap(a,b);
++a,++b;
int l=1,r=n-b,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(T.query(a,mid)==T.query(b,mid)) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
continue;
}
char ss[10];
int x,delta;
scanf("%d",&x);
scanf("%s",ss);
delta=ss[0]-'a';
if(opt[0]=='I') T.insert(x+1,delta),n+=1; //insert position: x+2 -> x+2
if(opt[0]=='R') T.update(x+1,delta); //actual position: x+1
}
return 0;
}

  

[JSOI2008]火星人 hash+splay的更多相关文章

  1. BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6243  Solved: 2007[Submit] ...

  2. BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )

    用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...

  3. BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8112  Solved: 2569[Submit] ...

  4. 【BZOJ1014】[JSOI2008]火星人prefix Splay+hash

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

  5. P4036 [JSOI2008]火星人(splay+hash+二分)

    P4036 [JSOI2008]火星人 Splay维护hash,查询二分 $a[x].vl=a[lc].vl*ha[a[rc].sz+1]+a[x].w*ha[a[rc].sz]+a[rc].vl$ ...

  6. BZOJ 1014: [JSOI2008]火星人prefix Splay+二分

    1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...

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

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

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

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

  9. 【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分

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

随机推荐

  1. [JZOJ NOIP2018模拟10.21]

    考试之前我刚刚领略到了特判的重要性,没想到T2的两个子任务还是写挂了,丢了20分 考试的感觉不行,一路打的都是暴力,正解的思路想到一半就断了推不下去 T1:逛公园 题目链接: https://jzoj ...

  2. 在Eclipse里连接Tomcat部署到项目(maven项目和web项目都适用)

    不多说,直接上干货! 前提, Tomcat *的下载(绿色版和安装版都适用) Tomcat *的安装和运行(绿色版和安装版都适用) Tomcat的配置文件详解 我这里以,manven项目为例,当然,w ...

  3. Excel中将字符串中从右起第n个指定字符替换的方法

    比如你想把www.baidu.com.cn中的倒数第二个”.”替换成@,则可以用: =SUBSTITUTE(A1,".","@",LEN(A1)-LEN(SUB ...

  4. etxjs

    序言 编辑 功能丰富,无人能出其右. 无论是界面之美,还是功能之强,ext的表格控件都高居榜首. 单选行,多选行,高亮显示选中的行,拖拽改变列宽度,按列排序,这些基本功能ExtJS轻量级实现. 自动生 ...

  5. java中三个类别加载器的关系以及各自加载的类的范围

    Java在需要使用类别的时候,才会将类别加载,Java的类别载入是由类别载入器(Class loader)来达到的,预设上,在程序启动之后,主要会有三个类别加载器:Bootstrap Loader.E ...

  6. swift语言点评二

    一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...

  7. QT+OpenCV进行图像采集最小时延能够达到20ms

    得到“算法高性能”项目的支持,目前成功地在Win10上运行WB2,感觉目前的代码速度慢.响应慢.CPU占用比例高.这种情况下3399上能够运行,说明这个平台已经是很强的了.下一步,首先在Windows ...

  8. 洛谷 P1026 统计单词个数 (分组+子串预处理)(分组型dp再次总结)

    一看完这道题就知道是划分型dp 有两个点要注意 (1)怎么预处理子串. 表示以i为开头,结尾在j之前(含),有没有子串,有就1,没有就0 (2)dp的过程 这种分成k组最优的题目已经高度模板化了,我总 ...

  9. Linux防火墙iptables安装配置--使用远程工具Xmanager和ftp服务安装配置

    一.linux关闭防火墙:    a.用户直接在终端输入:service iptables stop     查看防火墙状况:service iptables status  b.root用户在终端输 ...

  10. mysql索引的使用及优化方法

    数据库高级管理及优化 MySQL性能优化 优化MySQL数据库是数据库管理员和数据库开发人员的必备技能.优化MySQL,一方面是找出系统的瓶颈,提高MySQL数据库整体的性能:另一方面是合理设计结构和 ...