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、 询问。语法:Q x y,x, y均为正整数。功能:计算LCQ(x, y) 限制:1 <= x, y <= 当前字符串长度。 2、 修改。语法:R x d,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字符串长度。 3、 插入:语法:I x d,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

HINT

数据规模:

对于100%的数据,满足:

1、 所有字符串自始至终都只有小写字母构成。

2、 M <= 150,000

3、 字符串长度L自始至终都满足L <= 100,000

4、 询问操作的个数不超过10,000个。

对于第1,2个数据,字符串长度自始至终都不超过1,000

对于第3,4,5个数据,没有插入操作。

Source

 r_64大神犇告诉我splay+二分+哈希,因此我就写了一发,复杂度O(nlogn^2)。
代码:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std; #define rhl 37
#define maxl 100010
#define maxm 150010
char s[maxl]; int len;
unsigned long long mi[maxl+maxm]; struct SPLAY
{
int ch[maxl+maxm][],fa[maxl+maxm],size[maxl+maxm],key[maxl+maxm],cnt,root;
unsigned long long hash[maxl+maxm]; inline void updata(int x)
{
size[x] = size[ch[x][]] + size[ch[x][]] + ;
hash[x] = hash[ch[x][]] * mi[size[ch[x][]]+] + mi[size[ch[x][]]] * (key[x]) + hash[ch[x][]];
} inline void rotate(int x)
{
int y = fa[x],z = fa[y],l,r;
if (ch[y][] == x) l = ; else l = ; r = l ^ ;
if (z != )
{
if (ch[z][] == y) ch[z][] = x; else ch[z][] = x;
}
fa[x] = z; fa[y] = x; fa[ch[x][r]] = y;
ch[y][l] = ch[x][r]; ch[x][r] = y;
updata(y); updata(x);
fa[] = ; ch[][] = ch[][] = ;
} inline void splay(int x,int aim)
{
int t = fa[aim];
while (fa[x] != t)
{
int y = fa[x],z = fa[y];
if (fa[y] != t)
{
if ((ch[y][] == x)^(ch[z][] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
if (aim == root) root = x;
} inline int find(int rank,int have,int now)
{
if (size[ch[now][]]+have+==rank) return now;
if (size[ch[now][]]+have+ > rank) return find(rank,have,ch[now][]);
else return find(rank,have+size[ch[now][]]+,ch[now][]);
} inline void init(int l,int r,int f)
{
int p; cnt = ; root = ;
build(l,r,f);
p = find(len,,root);
splay(p,root);
fa[++cnt] = p;
ch[p][] = cnt;
key[cnt] = ;
size[cnt] = ;
updata(p);
p = find(,,root);
splay(p,root);
fa[++cnt] = p;
ch[p][] = cnt;
key[cnt] = ;
size[cnt] = ;
updata(p);
} inline void build(int l,int r,int f)
{
fa[++cnt] = f;
if (l == r)
{
key[cnt] = s[l-] - 'a' + ;
hash[cnt] = key[cnt];
size[cnt] = ;
return;
}
int mid = (l+r)>>,now = cnt;
key[now] = s[mid-] - 'a' + ;
if (mid > l)
{
ch[now][] = cnt + ;
build(l,mid - ,now);
}
if (mid < r)
{
ch[now][] = cnt+;
build(mid+,r,now);
}
updata(now);
} inline void change(int a,char b)
{
int p = find(a+,,root);
splay(p,root);
key[p] = b-'a'+;
updata(p);
} inline void insert(int a,char b)
{
int p = find(a+,,root),q = find(a+,,root);
splay(p,root);
splay(q,ch[p][]);
fa[++cnt] = q;
ch[q][] = cnt; key[cnt] = b-'a'+;
updata(cnt);
updata(q);
updata(p);
} inline unsigned long long calc(int a,int b)
{
int p = find(a,,root),q = find(b+,,root);
splay(p,root);
splay(q,ch[p][]);
return hash[ch[q][]];
}
}tree; inline int ask(int a,int b)
{
int l = ,r = min(len-a+,len-b+),mid;
while (l <= r)
{
mid = (l + r) >> ;
unsigned long long h1 = tree.calc(a,a+mid-),h2 = tree.calc(b,b+mid-);
if (h1 == h2)
l = mid+;
else r = mid - ;
}
return r;
} inline void ready()
{
mi[] = ;
for (int i = ;i < maxl+maxm;++i)
mi[i] = rhl*mi[i-];
} inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} int main()
{
freopen("1014.in","r",stdin);
freopen("1014.out","w",stdout);
scanf("%s",s); len = strlen(s);
ready();
tree.init(,len,);
int T = read(); char opt;
while (T--)
{
scanf("%c",&opt);
if (opt == 'Q')
{
int a = read(),b = read();
printf("%d\n",ask(a,b));
}
else if (opt == 'R')
{
int a = read(); char b; scanf("%c\n",&b);
tree.change(a,b);
}
else
{
int a = read(); char b; scanf("%c\n",&b);
tree.insert(a,b); ++len;
}
}
fclose(stdin); fclose(stdout);
return ;
}

BZOJ 1014 火星人prefix的更多相关文章

  1. bzoj 1014 火星人prefix - 链表 - 分块

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

  2. [BZOJ]1014 火星人prefix(JSOI2008)

    一边听省队dalao讲课一边做题真TM刺激. BZOJ的discuss简直就是题面plus.大样例.SuperHINT.dalao题解的结合体. Description 火星人最近研究了一种操作:求一 ...

  3. bzoj 1014 火星人prefix —— splay+hash

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用 splay 维护字符串上不同位置的哈希值还是第一次... 具体就是每个节点作为位置 ...

  4. BZOJ 1014 火星人 | 平衡树维护哈希

    BZOJ 1014 火星人 题意 有一个字符串,三中操作:在某位置后面插入一个字符.修改某位置的字符.询问两个后缀的最长公共前缀. 题解 看到网上的dalao们都说这道题是平衡树,我就很懵x--平衡树 ...

  5. 【BZOJ】【1014】【JLOI2008】火星人prefix

    Splay/二分/Hash 看了网上的题目关键字(都不用点进去看……我也是醉了)了解到做法= =那就上呗,前面做了好几道Splay的题就是为了练手搞这个的. Hash判断字符串是否相同应该很好理解吧? ...

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

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

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

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

  8. bzoj 1014: [JSOI2008]火星人prefix hash && splay

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

  9. 求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix

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

随机推荐

  1. HDU 4901(杭电多校训练#3 1005题)The Romantic Hero(DP)

    题目地址:HDU 4901 这题没想到最后竟然可以做出来.. .. 这题用了两次DP,先从前往后求一次异或的.再从后往前求一次与运算的. 各自是 1:求异或的时候,定义二维数组huo[1000][10 ...

  2. Delphi下实现全屏快速找图找色

    前言 最近有好几个朋友都在问我找图找色的问题,奇怪?于是乎写了一个专门用于找图找色的单元文件“BitmapData.pas”.在这个单元文件中我实现了从文件中导入位图.屏幕截图.鼠标指针截图.在图片上 ...

  3. js 解析 bytearray 成 字符串

    function bin2String(array) { return String.fromCharCode.apply(String, array); } var bit=[104,101,108 ...

  4. SA密钥长度、明文长度和密文长度

    本文介绍RSA加解密中必须考虑到的密钥长度.明文长度和密文长度问题,对第一次接触RSA的开发人员来说,RSA算是比较复杂的算法,RSA的复杂度是因为数学家把效率和安全也考虑进去的缘故. 本文先只谈密钥 ...

  5. Fragment之我的解决方案:Fragmentation

    Fragment系列文章:1.Fragment全解析系列(一):那些年踩过的坑2.Fragment全解析系列(二):正确的使用姿势3.Fragment之我的解决方案:Fragmentation 如果你 ...

  6. 使用SqlAlchemy时如何方便的取得dict数据、dumps成Json

    使用Sqlalchemy可以方便的从数据库读取出python对象形式的数据(吐槽:说实话对象形式也没多方便,还不如我之前从关系型数据库直接读取出dict形式的数据用起来方便,具体参见我以前的文章htt ...

  7. android学习笔记----JNI中的c控制java

    面向对象的底层实现 java作为面向对象高级语言,可对现实世界进行建模.和面向过程不同的是面向对象软件的编写不是流程的堆积,而是对业务逻辑的多视角分解和分类.其过程大致为:      1).将知识分解 ...

  8. linux系统下安装wget。

    我们先安装linux系统比如centos7.1里面有的就没有wget下载工具.wget这个命令就不可以使用. 我们使用 yum -y install wget yum install perl 会出现 ...

  9. linux设置语言编码

    前段时间在服务器上安装了centos6.2版本,当初安装时语言选择英文.这本来也没有什么问题,直到前一段时间.我的同事发现部署的web项目中出现乱码情况.但中文作为参数进行传递到下一个页面的时候就乱码 ...

  10. sublime的js调试环境(基于node环境)

    很多的语言都有控制台,都要专门的ide,js,用sublime在node的环境下,可以配置console, 如何配置?首先,要有node的环境,下载安装,还要安装到c盘才行,然后找到'工具(tool) ...