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. leetcode-91-解码方法(动态规划和递归两种解法)

    题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数 ...

  2. linux之getenv putenv setenv和unsetenv详解

    1.getenv函数 头文件:#include<stdlib.h> 函数原型: char * getenv(const char* name); 函数说明:getenv()用来取得参数na ...

  3. numpy.argmax()

    numpy.argmax(a, axis=None, out=None) 返回沿轴axis最大值的索引 Parameters: a : array_like                      ...

  4. Wi-Fi科普讲稿

    Wi-Fi 从入门到?? 组员:deleted 什么是Wi-Fi Wi-Fi 在中文里又称作"无线热点",是Wi-Fi联盟制造商的商标做为产品的品牌认证,是一个创建于IEEE 80 ...

  5. weiFenLuo.winFormsUI.Docking.dll学习

    引用方法: 1.建立一个WinForm工程,默认生成了一个WinForm窗体. 2.引用—>添加引用—>浏览—>weiFenLuo.winFormsUI.Docking.dll. 3 ...

  6. (转)错误"因为数据库正在使用,所以无法获得对数据库的独占访问权"的解决方案

    引发原因:是因为我在还原数据库的时候,还有其他的用户正在使用数据库,所以就会出现以上提示. 解决方法:1,设置数据库在单用户模式下工作.设置方法:在需要还原的数据库上右击,在右键菜单命令上选择&quo ...

  7. HelloStruts2

    第一个struts2项目: 前言 假 如 你 的 人 生 有 理 想,那 么 就 一 定 要 去 追,不 管 你 现 在 的 理 想 在 别 人 看 来是 多 么 的 可 笑 , 你 也 不 用 在 ...

  8. JDBC处理Transaction

    package com.ayang.jdbc; import java.sql.*; /** * transaction的构成,随便写一句insenrt,一执行executeUpdate(),它自动提 ...

  9. 第3章—高级装配—条件化的Bean

    条件化的Bean 通过活动的profile,我们可以获得不同的Bean.Spring 4提供了一个更通用的基于条件的Bean的创建方式,即使用@Conditional注解. @Conditional根 ...

  10. windows 老掉牙CMD的命令

    再老,也得温习下: net use \\' /user:'administrator' //ipc连接 net use \\127.0.0.1\ipc$ /delete //退出ipc net use ...