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. gitlab的简单使用

    mkdir folder //创建文件夹 cd folder/ git clone git连接 git status //git当前的状态 ls //当前目录下的所有文件 cd gitFolder 进 ...

  2. [java实现]常见算法之字符串操作

    一.字符串反转 把一个句子中的打次进行反转,比如“how are you” ,变为 “you are how” // 字符串反转 public class StringTest { // 字符反转的方 ...

  3. 能够放在文档的 <head> 中的各种配置元素

    一份关于任何可以写入到你的文档中 <head> 部分的清单 最小推荐 <meta charset="utf-8"> <meta http-equiv= ...

  4. Bootstrap-datepicker日期时间选择器的简单使用

    日期时间选择器 目前,bootstrap有两种日历.datepicker和datetimepicker,后者是前者的拓展. Bootstrap日期和时间组件: 使用示例: 从左到右依次是十年视图.年视 ...

  5. sql自查询各种状态数据总和

  6. RestTemplate--解决中文乱码

    [原文链接]:https://www.tecchen.xyz/rest-template-messycode.html 我的个人博客:https://www.tecchen.xyz 在开发扇贝-每日一 ...

  7. PTA 这是二叉搜索树吗?

    https://pintia.cn/problem-sets/994805046380707840/problems/994805070971912192 #include<iostream&g ...

  8. [转] gitlab 的 CI/CD 配置管理

    [From] http://blog.51cto.com/flyfish225/2156602 gitlab 的 CI/CD 配置管理 (二) 标签(空格分隔):运维系列 一:gitlab CI/CD ...

  9. 【实战】Weblogic反序列化Getshell

    修仙就是干,直接操作起来 1.访问http://x.x.x.x:7001/wls-wsat/CoordinatorPortType 2.加入Content-Type:text/xml 3.在body中 ...

  10. (转)Mysql数据库之Binlog日志使用总结

    使用mysqlbinlog提取二进制日志 原文:http://blog.csdn.net/leshami/article/details/41962243 MySQL binlog日志记录了MySQL ...