Description

  火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。比方说,有这样一个字符串:madamimadam,
我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m 现在,
火星人定义了一个函数LCQ(x, y),表示:该字符串中第x个字符开始的字串,与该字符串中第y个字符开始的字串
,两个字串的公共前缀的长度。比方说,LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0 在研究LCQ函数的过程
中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出LCQ函数的值;同样,
如果求出了LCQ函数的值,也可以很快地将该字符串的后缀排好序。 尽管火星人聪明地找到了求取LCQ函数的快速
算法,但不甘心认输的地球人又给火星人出了个难题:在求取LCQ函数的同时,还可以改变字符串本身。具体地说
,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此
复杂的问题中,火星人是否还能够做到很快地求取LCQ函数的值。

Input

  第一行给出初始的字符串。第二行是一个非负整数M,表示操作的个数。接下来的M行,每行描述一个操作。操
作有3种,如下所示
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

  对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。

Sample Input

madamimadam
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11

Sample Output

5
1
0
2
1

解题思路:

假如给你一个不变的序列,求最长公共前缀可以二分check()

splay维护hash

hash的本质就是前面的部分*后面的长度+后面的部分。

splay维护一下子树hash值就好了。

代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define lll tr[spc].ch[0]
#define rrr tr[spc].ch[1]
#define ls ch[0]
#define rs ch[1]
typedef unsigned long long lnt;
const int N=;
const lnt M=9827601ull;
struct trnt{
int ch[];
int fa;
int wgt;
lnt val;
lnt hax;
}tr[N];
int n,m;
int root;
int siz;
lnt mal[N];
char lne[N];
char cmd[];
lnt a[N];
bool whc(int spc)
{
return tr[tr[spc].fa].rs==spc;
}
void pushup(int spc)
{
tr[spc].wgt=tr[lll].wgt+tr[rrr].wgt+;
tr[spc].hax=tr[lll].hax*mal[tr[rrr].wgt+]+tr[spc].val*mal[tr[rrr].wgt]+tr[rrr].hax;
return ;
}
int place(int spc,int rnk)
{
if(tr[lll].wgt>=rnk)
return place(lll,rnk);
if(tr[lll].wgt+==rnk)
return spc;
return place(rrr,rnk-tr[lll].wgt-);
}
void rotate(int spc)
{
int f=tr[spc].fa;
bool k=whc(spc);
tr[f].ch[k]=tr[spc].ch[!k];
tr[spc].ch[!k]=f;
tr[tr[f].fa].ch[whc(f)]=spc;
tr[spc].fa=tr[f].fa;
tr[f].fa=spc;
tr[tr[f].ch[k]].fa=f;
pushup(f);
pushup(spc);
return ;
}
void splay(int spc,int f)
{
while(tr[spc].fa!=f)
{
int ft=tr[spc].fa;
if(tr[ft].fa==f)
{
rotate(spc);
break;
}
if(whc(spc)^whc(ft))
rotate(spc);
else
rotate(ft);
rotate(spc);
}
if(!f)
root=spc;
}
void build(int l,int r,int &spc,int f)
{
if(l>r)
return ;
spc=++siz;
int mid=(l+r)>>;
tr[spc].fa=f;
tr[spc].val=a[mid];
build(l,mid-,lll,spc);
build(mid+,r,rrr,spc);
pushup(spc);
return ;
}
bool LCQ(int la,int lb,int len)
{
if(la+len->n||lb+len->n)
return false;
splay(place(root,la),);
splay(place(root,la+len+),root);
int spc=tr[tr[root].rs].ls;
lnt a1=tr[spc].hax;
splay(place(root,lb),);
splay(place(root,lb+len+),root);
spc=tr[tr[root].rs].ls;
if(tr[spc].hax==a1)
return true;
return false;
}
int main()
{
scanf("%s",lne+);
n=strlen(lne+);
mal[]=;
for(int i=;i<N;i++)
{
mal[i]=mal[i-]*M;
a[i]=lne[i];
}
scanf("%d",&m);
build(,n+,root,);
while(m--)
{
scanf("%s",cmd);
if(cmd[]=='Q')
{
int sa,sb;
scanf("%d%d",&sa,&sb);
int ans=;
int l,r;
l=;r=siz-;
while(l<=r)
{
int mid=(l+r)>>;
if(LCQ(sa,sb,mid))
{
l=mid+;
ans=mid;
}else
r=mid-;
}
printf("%d\n",ans);
}else if(cmd[]=='R')
{
int pos;
scanf("%d",&pos);
scanf("%s",cmd);
splay(place(root,pos+),);
tr[root].val=cmd[];
pushup(root);
}else if(cmd[]=='I')
{
siz++;
n++;
int pos;
scanf("%d",&pos);
scanf("%s",cmd);
splay(place(root,pos+),);
splay(place(root,pos+),root);
int spc=tr[root].rs;
tr[siz].val=cmd[];
tr[siz].fa=spc;
tr[spc].ls=siz;
pushup(siz);
pushup(spc);
pushup(root);
}
}
return ;
}

BZOJ1014[JSOI2008]火星人prefix(splay维护hash)的更多相关文章

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

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

  2. [bzoj1014](JSOI2008)火星人 prefix (Splay维护哈希)

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

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

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

  4. 【BZOJ-1014】火星人prefix Splay + 二分 + Hash

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

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

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

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

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

  7. bzoj1014: [JSOI2008]火星人prefix splay+hash

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

  8. BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值

    题目: 题解: #include<cstdio> #include<algorithm> #include<cstring> typedef long long l ...

  9. [BZOJ1014][JSOI2008]火星人prefix

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

随机推荐

  1. LeetCode_Construct Binary Tree from Inorder and Postorder Traversal

    一.题目 Construct Binary Tree from Inorder and Postorder Traversal My Submissions Given inorder and pos ...

  2. 思考一下activity的启动模式

    在android里,有4种activity的启动模式.分别为:"standard" (默认) "singleTop" "singleTask" ...

  3. 简单缓存Cache

    接口 interface ICache { /// <summary> /// 添加 /// </summary> /// <param name="key&q ...

  4. HDU 4358 Boring counting dfs序+莫队算法

    题意:N个节点的有根树,每个节点有一个weight.有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种. 这是第一次写莫队算法,之前也只是偶有耳闻. 看了别人的代码打的,还是贴上 ...

  5. BZOJ2137: submultiple(生成函数,二项式定理)

    Description 设函数g(N)表示N的约数个数.现在给出一个数M,求出所有M的约数x的g(x)的K次方和. Input 第一行输入N,K.N表示M由前N小的素数组成.接下来N行,第i+1行有一 ...

  6. Springboot 获取yml、properties参数

    获取properties或yml文件的配置数据(两种方法)(默认的application文件或者自定义的yml和properties) 1.使用@Value()注解 1.1 配置数据 如:在prope ...

  7. 03014_properties配置文件

    1.使用properties配置文件 (1)开发中获得连接的4个参数(驱动.URL.用户名.密码)通常都存在配置文件中,方便后期维护,程序如果需要更换数据库,只需要修改配置文件即可: (2)通常情况下 ...

  8. c#+windows api SetWindowsHookEx 全局钩子 demo 下载

    效果图 源代码下载地址: http://download.csdn.net/detail/dhfekl/7522141

  9. UVA - 10032 Tug of War (二进制标记+01背包)

    Description Problem F: Tug of War A tug of war is to be arranged at the local office picnic. For the ...

  10. vim 帮助文档汉化

    我们在使用vim 的时候,经常会碰到一些陌生的命令,这时候我们可以通过 :help *** 来查询一些命令的使用方法.不过呢,我们安装的vim默认的都是英文的,看起来很费劲,尤其是向我这样英语比较差的 ...