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

HINT

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

2、M<=150,000

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

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

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

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

题解

用平衡树维护字符串hash,查询时二分。

不自然溢出的话在bzoj上会爆。

代码:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
typedef long long LL;
inline int readInt() {
int ans = 0, c;
while (!isdigit(c = getchar()));
do ans = ans * 10 + c - '0';
while (isdigit(c = getchar()));
return ans;
}
const int base = 31;
const int N = 100050;
int pbase[N];
inline int getHash(int h1, int h2, int l2) {
return h1 * pbase[l2] + h2;
}
struct Splay{
int son[N][2], fa[N];
int v[N], len[N], hash[N];
int root, cnt;
inline void upd(int x) {
int lc = son[x][0], rc = son[x][1];
len[x] = len[lc] + len[rc] + 1;
hash[x] = getHash(getHash(hash[lc], v[x], 1), hash[rc], len[rc]);
}
inline int dir(int x) { return son[fa[x]][1] == x; }
inline void rotate(int x) {
int f = fa[x], d = dir(x);
if (son[f][d] = son[x][d ^ 1])
fa[son[f][d]] = f;
if (fa[x] = fa[f])
son[fa[f]][dir(f)] = x;
upd(son[fa[f] = x][d ^ 1] = f);
upd(x);
}
void splay(int x, int p = 0) {
for (; fa[x] != p; rotate(x))
if (fa[fa[x]] != p) rotate(dir(fa[x]) == dir(x) ? fa[x] : x);
if (!p) root = x;
}
void kth(int k, int x = -1) {
if (x == -1) x = root;
int y = fa[x];
while (k != len[son[x][0]]) {
if (k < len[son[x][0]]) {
x = son[x][0];
} else {
k -= len[son[x][0]] + 1;
x = son[x][1];
}
}
splay(x, y);
}
void insert(int k, int w) {
int p = ++cnt;
v[p] = w;
kth(k);
if (!root) {
root = p;
} else {
if (son[p][1] = son[root][1])
fa[son[p][1]] = p;
if (son[p][0] = root)
fa[root] = p;
son[root][1] = 0;
upd(root);
upd(root = p);
}
}
inline void modify(int k, int w) {
kth(k);
v[root] = w;
upd(root);
}
inline int query(int l, int r) {
kth(r + 1);
kth(l - 1, son[root][0]);
return hash[son[son[root][0]][1]];
}
};
Splay T; int n, m;
char s[N];
bool check(int a, int b, int l) {
return T.query(a, a + l - 1) == T.query(b, b + l - 1);
}
int query(int a, int b) {
int l = 0, r = n - std::max(a, b) - 1;
while (l < r) {
int mid = r + (l - r) / 2;
if (check(a, b, mid)) l = mid;
else r = mid - 1;
}
return l;
}
int main() {
pbase[0] = 1;
for (int i = 1; i < N; ++i) pbase[i] = (LL)pbase[i - 1] * base;
scanf("%s", s + 1);
n = strlen(s + 1) + 2;
s[0] = s[n - 1] = 'z' + 1;
for (int i = 0; i < n; ++i) T.insert(std::max(0, i - 1), s[i] - 'a');
scanf("%d", &m);
while (m--) {
int x;
while (!isalpha(*s = getchar()));
if (*s == 'Q') {
printf("%d\n", query(readInt(), readInt()));
} else if (*s == 'R') {
x = readInt();
while (!isalpha(*s = getchar()));
T.modify(x, *s - 'a');
} else {
x = readInt();
while (!isalpha(*s = getchar()));
T.insert(x, *s - 'a');
++n;
}
}
return 0;
}

  

BZOJ1014 [JSOI2008]火星人的更多相关文章

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

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

  2. BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*

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

  3. 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)

    [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...

  4. bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash 带修改带插入:splay维护 #include< ...

  5. [bzoj1014][JSOI2008]火星人prefix_非旋转Treap_hash_二分

    火星人prefix bzoj-1014 JSOI-2004 题目大意:给定一个字符串,支持三种操作:1.查询:两个后缀之间的$LCP$:2.单点修改:3.插入一个字符. 注释:$1\le n\le 1 ...

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

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

  7. BZOJ1014[JSOI2008]火星人——非旋转treap+二分答案+hash

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

  8. BZOJ1014 JSOI2008火星人(splay+哈希)

    splay维护哈希值即可. #include<iostream> #include<cstdio> #include<cmath> #include<cstd ...

  9. BZOJ1014:[JSOI2008]火星人(Splay,hash)

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

随机推荐

  1. idea部署tomcat:tomee required to support ear/ejb de。。

    要确定artifact 的type是使用的web application :exploded类型,使用javaee application :exploded就会报这个错误.

  2. delphi 10.2 ----简单的递归函数例子求和

    unit Unit10; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...

  3. SAE实践——创建简单留言板

    1. 创建数据库 将mysaeapptest的数据库初始化之后,建立新的数据库,在PHPMyAdmin输入下列SQL. CREATE TABLE IF NOT EXISTS `ny_diary` ( ...

  4. Django分页类的封装

    Django分页类的封装 Django ORM  封装 之前有提到(Django分页的实现)会多次用到分页,将分页功能封装起来能极大提高效率. 其实不是很难,就是将之前实现的代码全都放到类中,将需要用 ...

  5. 题解51nod1515——明辨是非

    前提 在这道题老师讲过之后,再考时,我还是WA了ヽ(*.>Д<)o゜果然,我还是好菜啊~%?…,# *'☆&℃$︿★? 谢谢Jack_Pei dalao的帮忙.~~O(∩_∩)O~ ...

  6. mysql使用常见问题

    常见问题之一: 启动mysql时显示: The server quit without updating PID file 1.可能是/usr/local/mysql/data/mysql.pid文件 ...

  7. 【第2次会议记录_2018.5.27】—— [ 算法原理 ]:手工特征提取的概念问题。(by_wanghao)

    1.提取 特征点 .特征描述子 与 提取特征向量 之间的区别: (1).特征点:指的是一张图片上比较有代表性的‘位置’,提取特征点就是把图片中这些有代表性的位置给标出来. (2).特征描述子:当提取出 ...

  8. MVC3权限验证,诡异的OnAuthorization

    mvc3权限验证 protected override void OnAuthorization(AuthorizationContext filterContext) { if (//开始权限验证返 ...

  9. 什么是Java代码的编译与反编译?(转)

    转自:http://java.tedu.cn/ask/203119.html Java代码的编译与反编译 一.什么是编译 1.利用编译程序从源语言编写的源程序产生目标程序的过程. 2.用编译程序产生目 ...

  10. 关于c#中委托与事件的一些理解

    文章目的:作者(初学者)在学习c#的过程中,对事件.委托及其中的“object sender,EventArgs e”一直感觉理解不透,因此在网上找了一些资料,学习并整理出了该篇笔记,希望能将自己的心 ...