题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符。

  前几天刚学了hash+二分求lcp,就看到这题。

  原来splay还能这么用?!原来splay模板这么好写?我以前写的splay是假的吧woc

  splay每个节点代表一个字符,并维护这个子树代表一个子串的哈希值。因为splay旋转不破坏树结构,所以不论怎么旋转这棵splay树都能代表这个字符串。

  预处理处理少了调了半天呜呜呜

  赶紧跑去更新自己的splay模板

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#define which(x) (son[fa[x]][1]==x)
#define ull unsigned long long
using namespace std;
const int maxn=;
int n,m,x,y,tot,root;
int fa[maxn],son[maxn][],size[maxn],s[maxn];
ull hs[maxn],mul[maxn];
char st[maxn],ch[maxn],ch2[maxn];
void read(int &k)
{
int f=;k=;char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(c<=''&&c>='')k=k*+c-'',c=getchar();
k*=f;
}
void update(int x)
{
size[x]=size[son[x][]]+size[son[x][]]+;
hs[x]=hs[son[x][]]+mul[size[son[x][]]]*s[x]+mul[size[son[x][]]+]*hs[son[x][]];
}
void rotate(int x)
{
int f=fa[x];
bool k=which(x);
son[f][k]=son[x][!k];
son[x][!k]=f;
son[fa[f]][which(f)]=x;
fa[son[f][k]]=f;
fa[x]=fa[f];
fa[f]=x;
size[x]=size[f];
hs[x]=hs[f];
update(f);
}
void splay(int x,int g)
{
while(fa[x]!=g)
{
int f=fa[x];
if(fa[f]==g)
{
rotate(x);
break;
}
if(which(x)^which(f))rotate(x);
else rotate(f);
rotate(x);
}
if(!g)root=x;
}
int rank(int x,int k)
{
if(k<=size[son[x][]])return rank(son[x][],k);
else if(k==(size[son[x][]]+))return x;
else return rank(son[x][],k-size[son[x][]]-);
}
int build(int l,int r,int f)
{
if(l>r)return ;
int x=++tot,mid=(l+r)>>;
fa[x]=f;s[x]=(int)(st[mid]-'a')+;
son[x][]=build(l,mid-,x);
son[x][]=build(mid+,r,x);
update(x);
return x;
}
void insert(int k,int ch)
{
int x=rank(root,k),y=rank(root,k+);
splay(x,);splay(y,x);
s[++tot]=ch;
fa[tot]=y;son[y][]=tot;
update(tot);update(y);update(x);
}
void change(int k,int ch)
{
int x=rank(root,k);
splay(x,);
s[x]=ch;
update(x);
}
int lcp(int kx,int ky)
{
int l=,r=n;
while(l<r)
{
int mid=(l+r+)>>;
if(ky+mid>n+)
{
r=mid-;
continue;
}
int x=rank(root,kx-),y=rank(root,kx+mid);
splay(x,);splay(y,x);
ull haxi=hs[son[y][]];
x=rank(root,ky-),y=rank(root,ky+mid);
splay(x,);splay(y,x);
if(haxi==hs[son[y][]])l=mid;
else r=mid-;
}
return l;
}
int main()
{
scanf("%s",st+);
n=strlen(st+);
mul[]=;
for(int i=;i<=;i++)mul[i]=mul[i-]*;
root=build(,n+,);
read(m);
for(int i=;i<=m;i++)
{
scanf("%s",ch);
if(ch[]=='Q')
{
read(x);read(y);
if(x>y)swap(x,y);
if(x!=y)printf("%d\n",lcp(x+,y+));
else printf("%d\n",n-x+);
}
if(ch[]=='R')
{
read(x);
scanf("%s",ch2);
change(x+,(int)(ch2[]-'a')+);
}
if(ch[]=='I')
{
read(x);
scanf("%s",ch2);
insert(x+,(int)(ch2[]-'a')+);
n++;
}
}
return ;
}

bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)的更多相关文章

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

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

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

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

  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

    我写的代码好像自古以来就是bzoj不友好型的 本地跑的比std快,但是交上去巧妙被卡 答案...应该是对的,拍了好久了 #include <bits/stdc++.h> #define M ...

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

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

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

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

  7. 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$ ...

  8. BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)

    题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...

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

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

  10. BZOJ1014[JSOI2008]火星人prefix(splay维护hash)

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

随机推荐

  1. 用最简单的MVC模式输出内容

    MVC是模型(model)-视图(view)-控制器(controller)的缩写,它的作用是使代码分离,可维护性高.重用性高 编写Model层: <?php class model{ publ ...

  2. 获取ip地址以及获取城市等信息

    class Program { static void Main(string[] args) { string ip = GetIP(); if (ip != null) { string city ...

  3. Struts2(十.在修改页显示照片列表并增加删除照片功能)

    一.显示照片列表功能 struts2中一般的处理方式:先在action中,准备数据,转到jsp中显示 1.UserAction /** * 点击修改用户按钮跳转到修改用户界面 * 为用户准备照片,以便 ...

  4. lintcode 平面列表

    问题描述: 给定一个列表,该列表中的每个要素要么是个列表,要么是整数.将其变成一个只包含整数的简单列表. 样例: 给定 [1,2,[1,2]],返回 [1,2,1,2]. 给定 [4,[3,[2,[1 ...

  5. 吴恩达深度学习 反向传播(Back Propagation)公式推导技巧

    由于之前看的深度学习的知识都比较零散,补一下吴老师的课程希望能对这块有一个比较完整的认识.课程分为5个部分(粗体部分为已经看过的): 神经网络和深度学习 改善深层神经网络:超参数调试.正则化以及优化 ...

  6. opencv-学习笔记(2)

    opencv-学习笔记(2) 这章记录了 获取像素点,改变像素点 获取图像的属性(行,列,通道数,数据类型) roi感应区 拆分以及合并图像通道 边缘扩充 opencv获取像素点,改变像素点 ---- ...

  7. [C++] Fucntions

    Statements A break statements terminate the nearest wile, do while, for or switch statement. A break ...

  8. 《javascript模式--by Stoyan Stefanov》书摘--函数

    三.函数 1.函数的命名属性 // IE下不支持name属性 var foo = function bar () { // todo }; foo.name; // "bar" 2 ...

  9. linux下的常用技巧。

    xargs  linux下的多行合并~ [root@]# yum list installed|grep php|awk -F ' ' '{print $1}' php-channel-nrk.noa ...

  10. # ML学习小笔记—Classification

    关于本课程的相关资料http://speech.ee.ntu.edu.tw/~tlkagk/courses_ML17.html 通过模型可以分类输入,此时根据分类结果的正确与否会有一个Loss函数.找 ...