BZOJ 1014 火星人prefix
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
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11
Sample Output
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
#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的更多相关文章
- bzoj 1014 火星人prefix - 链表 - 分块
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- [BZOJ]1014 火星人prefix(JSOI2008)
一边听省队dalao讲课一边做题真TM刺激. BZOJ的discuss简直就是题面plus.大样例.SuperHINT.dalao题解的结合体. Description 火星人最近研究了一种操作:求一 ...
- bzoj 1014 火星人prefix —— splay+hash
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用 splay 维护字符串上不同位置的哈希值还是第一次... 具体就是每个节点作为位置 ...
- BZOJ 1014 火星人 | 平衡树维护哈希
BZOJ 1014 火星人 题意 有一个字符串,三中操作:在某位置后面插入一个字符.修改某位置的字符.询问两个后缀的最长公共前缀. 题解 看到网上的dalao们都说这道题是平衡树,我就很懵x--平衡树 ...
- 【BZOJ】【1014】【JLOI2008】火星人prefix
Splay/二分/Hash 看了网上的题目关键字(都不用点进去看……我也是醉了)了解到做法= =那就上呗,前面做了好几道Splay的题就是为了练手搞这个的. Hash判断字符串是否相同应该很好理解吧? ...
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6243 Solved: 2007[Submit] ...
- BZOJ 1014: [JSOI2008]火星人prefix Splay+二分
1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...
- bzoj 1014: [JSOI2008]火星人prefix hash && splay
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3154 Solved: 948[Submit][ ...
- 求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4164 Solved: 1277[Submit] ...
随机推荐
- WinForm中关于控件焦点的问题
方法一: 在打开一个窗体时,我们往往需要设置焦点让光标出现在我们希望它出现的位置上. 这时我们可以在窗体的Activated事件中设置焦点 例如我们希望光标在打开窗体的时候出现在textBox1上,我 ...
- 写在新建博客的第一天 分类: fool_tree的笔记本 2014-11-08 17:57 144人阅读 评论(0) 收藏
来CSDN开博客的目的有两个: 其一是因为CSDN的代码输出,看过一些博文,觉得这里的代码输出真的很漂亮: 其二则是因为,感觉自己印象笔记用久了之后,渐渐地几乎不再自己写些东西了,习惯了方便的剪藏插件 ...
- [PWA] sw-precache
Link to CodeLab In this codelab, we'll retrace those steps but this time we'll use a tool called sw- ...
- [转]C#中yield用法
yield 关键字向编译器指示它所在的方法是迭代器块.编译器生成一个类来实现迭代器块中表示的行为.在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值.这是一个返回值, ...
- LaTeX 中插入数学公式
一.常用的数学符号 1.小写希腊字母 \alpha \nu \beta \xi \gamma o \delta \pi \epsilon \rho \zeta \sigma \eta \tau \th ...
- MongoDB 重启之后无法连接问题
困扰了一段时间的问题终于知道原因了,每次重启MongoDB服务器就会导致无法启动服务. 通过观察发现,每一次重启完了之后,MongoDB 会进行内存数据加载,而原来服务器内存配置过低,因此导致内存加载 ...
- 关于UNION和UNION ALL的区别
今天在运行程序的时候发现个问题,就是计算和的时候两条数据一样的话自动去除重复的,可是我这个程序需要重复的数据也算进来呀,然后就找原因,最后在sql语句中找到了是union和union all的问题,简 ...
- asp.net 批量下载实现(打包压缩下载)
1.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default ...
- power desinger 学习笔记<八>
转-PowerDesigner 把Comment复制到name中和把name复制到Comment 在使用PowerDesigner对数据库进行概念模型和物理模型设计时,一般在NAME或Comment中 ...
- 【转】iOS8 推送 获取 devicetoken
标签:推送 push ios8 devicetoken token xcode6 原文:http://roybaby.blog.51cto.com/1508945/1557854 打开AppDeleg ...