1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 8112  Solved: 2569
[Submit][Status][Discuss]

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个数据,没有插入操作。

Source

析:首先由于有插入还有修改,操作,用splay就可以解决,然后就是求lcp,这个可以用二分+Hash来解决,所以组合起来就好。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int maxn = 1e5 + 10;
const int maxm = 3e5 + 10;
const ULL mod = 3;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} #define Key_value ch[ch[root][1]][0]
int pre[maxn], ch[maxn][2], key[maxn], sz[maxn];
int root, tot1;
int s[maxn], tot2;
char a[maxn];
ULL H[maxn], xp[maxn]; void NewNode(int &rt, int fa, int x){
if(tot2) rt = s[tot2--];
else rt = ++tot1;
pre[rt] = fa;
key[rt] = x;
ch[rt][0] = ch[rt][1] = 0;
sz[rt] = 1;
} void push_up(int rt){
int l = ch[rt][0], r = ch[rt][1];
sz[rt] = sz[l] + sz[r] + 1;
H[rt] = H[r] + key[rt] * xp[sz[r]] + H[l] * xp[sz[r]+1];
} void update_setv(int rt, int val){
if(!rt) return ;
key[rt] = val;
push_up(rt);
} void Build(int &rt, int l, int r, int fa){
if(l > r) return ;
int m = l+r >> 1;
NewNode(rt, fa, a[m]);
Build(ch[rt][0], l, m-1, rt);
Build(ch[rt][1], m+1, r, rt);
push_up(rt);
} void Init(){
tot1 = root = tot2 = 0;
ch[root][0] = ch[root][1] = sz[root] = pre[root] = 0;
key[root] = 0;
scanf("%s", a); n = strlen(a);
NewNode(root, 0, -1);
NewNode(ch[root][1], root, -1);
Build(Key_value, 0, n-1, ch[root][1]);
push_up(ch[root][1]);
push_up(root);
} int Get_kth(int rt, int k){
int t = sz[ch[rt][0]] + 1;
if(t == k) return rt;
if(t > k) return Get_kth(ch[rt][0], k);
return Get_kth(ch[rt][1], k-t);
} void Rotate(int x, int k){
int y = pre[x];
ch[y][!k] = ch[x][k];
pre[ch[x][k]] = y;
if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y] = x;
pre[x] = pre[y];
ch[x][k] = y;
pre[y] = x;
push_up(y);
} void Splay(int rt, int goal){
while(pre[rt] != goal){
if(pre[pre[rt]] == goal){
Rotate(rt, ch[pre[rt]][0] == rt);
continue;
}
int y = pre[rt];
int k = ch[pre[y]][0] == y;
if(ch[y][k] == rt){
Rotate(rt, !k);
Rotate(rt, k);
}
else{
Rotate(y, k);
Rotate(rt, k);
}
}
push_up(rt);
if(goal == 0) root = rt;
} void Insert(){
int pos, tot = 1;
scanf("%d", &pos);
scanf("%s", a);
Splay(Get_kth(root, pos+1), 0);
Splay(Get_kth(root, pos+2), root);
Build(Key_value, 0, tot-1, ch[root][1]);
push_up(ch[root][1]);
push_up(root);
++n;
} bool judge(int x, int y, int mid){
Splay(Get_kth(root, x), 0);
Splay(Get_kth(root, x + mid + 1), root);
ULL ans = H[Key_value];
Splay(Get_kth(root, y), 0);
Splay(Get_kth(root, y + mid + 1), root);
return ans == H[Key_value];
} int query(){
int x, y; scanf("%d %d", &x, &y);
int l = 1, r = min(n - x + 1, n - y + 1);
while(l <= r){
int m = l + r >> 1;
if(judge(x, y, m)) l = m + 1;
else r = m - 1;
}
return l - 1;
} void Make_setv(){
int pos, tot = 1;
scanf("%d", &pos);
scanf("%s", a);
Splay(Get_kth(root, pos), 0);
Splay(Get_kth(root, pos+tot+1), root);
update_setv(Key_value, a[0]);
push_up(ch[root][1]);
push_up(root);
} int main(){
xp[0] = 1;
for(int i = 1; i < maxn; ++i) xp[i] = xp[i-1] * mod;
Init();
scanf("%d", &m);
char op[5];
while(m--){
scanf("%s", op);
if(op[0] == 'Q') printf("%d\n", query());
else if(op[0] == 'R') Make_setv();
else Insert();
}
return 0;
}

  

BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)的更多相关文章

  1. BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )

    用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...

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

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

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

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

  4. 【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分

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

  5. BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)

    题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度 乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾) 看题解才知道这是一道splay题,首先要对s ...

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

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

  7. bzoj 1014 [JSOI2008]火星人prefix——splay+哈希

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用splay维护字符串,每个点记录子树的哈希值,然后二分查询. 二分不是把两个点的哈希 ...

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

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

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

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

随机推荐

  1. vim nginx配置文件时具备语法高亮功能

    1.下载nginx.vim 下载页面:http://www.vim.org/scripts/script.php?script_id=1886 wget http://www.vim.org/scri ...

  2. Linux启动提示Kernel panic - not syncing: Attempted to kill init解决办法

    系统类型:CentOS 6.5(x64) 启动提示:Kernel panic - not syncing: Attempted to kill init 解决办法: 系统启动的时候,按下‘e’键进入g ...

  3. 群晖Nas中搭建Intellij Idea的LicenseServer服务

    下载IntelliJIDEALicenseServer(直接找度娘) 准备 shellX 或其他 ssh工具,个人比较喜欢 mobaxterm. 通过 ssh工具连接到群晖中,用户名和密码就是登陆群晖 ...

  4. Centos7 vnc

    这是一个关于怎样在你的 CentOS 7 上安装配置 VNC 服务的教程.当然这个教程也适合 RHEL 7 .在这个教程里,我们将学习什么是 VNC 以及怎样在 CentOS 7 上安装配置 VNC ...

  5. springmvc处理url请求步骤

    处理步骤: 1.调用DispatcherServlet的doService方法 2.进入DispatchServlet的doDispatch方法 3.进入DispatchServlet的getHand ...

  6. Web标准:九、CSS表单设计

    Web标准:九.CSS表单设计 知识点: 1.改变文本框和文本域样式 2.用图片美化按钮 3.改变下拉列表样式 4.用label标签提升用户体验   1)改变文本框和文本域样式 文本框标签:<i ...

  7. Windows下Fortran编译Lapack库及使用的方法(转自新浪)

    Lapack 是一套被非常广泛使用的线性代数计算工具库,现在的主要编写语言时 Fortran90 .Lapack 基本上可以说是速度最快的线性代数计算库,我们看看官方给出的其被应用的几个地方就可以看出 ...

  8. Python3 len()方法

    Python3 len()方法  Python3 字符串 描述 Python len() 方法返回对象(字符.列表.元组等)长度或项目个数. 语法 len()方法语法: len( s ) 参数 s - ...

  9. Hibernate一级缓存(补)

    ------------------siwuxie095                                 什么是缓存         缓存是介于应用程序和永久性数据存储源(如:硬盘上的 ...

  10. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...