bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix
http://www.lydsy.com/JudgeOnline/problem.php?id=1014
两个后缀的最长公共前缀:二分+hash
带修改带插入:splay维护
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #define L 100001
- typedef unsigned long long ULL;
- using namespace std;
- char s[L+];
- int tot,root;
- int fa[L+],ch[L+][],siz[L+],key[L+];
- ULL has[L+];
- ULL bit[L+];
- int len;
- void read(int &x)
- {
- x=; char c=getchar();
- while(!isdigit(c)) c=getchar();
- while(isdigit(c)) { x=x*+c-''; c=getchar();}
- }
- void update(int x)
- {
- siz[x]=siz[ch[x][]]+siz[ch[x][]]+;
- has[x]=has[ch[x][]]*bit[siz[ch[x][]]+]+key[x]*bit[siz[ch[x][]]]+has[ch[x][]];
- }
- void build(int l,int r,int f)
- {
- if(l>r) return;
- int mid=l+r>>;
- ch[f][mid>f]=mid;
- fa[mid]=f;
- siz[mid]=; key[mid]=s[mid]-'a'+;
- build(l,mid-,mid);
- build(mid+,r,mid);
- update(mid);
- }
- int find(int x)
- {
- int now=root;
- while()
- {
- if(siz[ch[now][]]>=x) now=ch[now][];
- else
- {
- x-=siz[ch[now][]];
- if(x==) return now;
- x--; now=ch[now][];
- }
- }
- }
- int findsuf(int x)
- {
- int now=ch[x][];
- while(ch[now][]) now=ch[now][];
- return now;
- }
- void rotate(int x,int &goal)
- {
- int y=fa[x],z=fa[y],k=ch[y][]==x;
- if(y!=goal) ch[z][ch[z][]==y]=x;
- else goal=x;
- ch[y][k]=ch[x][k^]; ch[x][k^]=y;
- fa[x]=z; fa[y]=x; fa[ch[y][k]]=y;
- update(y);
- }
- bool getson(int x)
- {
- return ch[fa[x]][]==x;
- }
- void splay(int x,int &goal)
- {
- int y;
- while(x!=goal)
- {
- y=fa[x];
- if(y==goal) rotate(x,goal);
- else
- {
- if(getson(x)^getson(y)) rotate(x,goal);
- else rotate(y,goal);
- rotate(x,goal);
- }
- update(x);
- }
- }
- ULL query(int l,int r)
- {
- if(r==len+)
- {
- int t1=find(l-);
- splay(t1,root);
- return has[ch[root][]];
- }
- int tl=find(l-),tr=find(r+);
- splay(tl,root);
- splay(tr,ch[root][]);
- return has[ch[tr][]];
- }
- int main()
- {
- //freopen("bzoj_1014.in","r",stdin);
- //freopen("bzoj_1014.out","w",stdout);
- scanf("%s",s+); len=strlen(s+);
- int m; read(m);
- bit[]=; for(int i=;i<L;++i) bit[i]=bit[i-]*;
- build(,len+,);
- root=len+>>;
- char c[]; int x,y;
- int l,r,mid,ans;
- tot=len+;
- while(m--)
- {
- scanf("%s",c);
- if(c[]=='Q')
- {
- read(x); read(y);
- x++; y++;
- ans=; l=; r=min(len+-x+,len+-y+);
- while(l<=r)
- {
- mid=l+r>>;
- if(query(x,x+mid-)!=query(y,y+mid-)) r=mid-;
- else l=mid+,ans=mid;
- }
- cout<<ans<<'\n';
- }
- else if(c[]=='R')
- {
- read(x); scanf("%s",c);
- x++;
- x=find(x);
- splay(x,root);
- key[x]=c[]-'a'+;
- }
- else
- {
- read(x); scanf("%s",c);
- x++;
- x=find(x); splay(x,root);
- y=findsuf(x);
- if(y)
- {
- splay(y,ch[x][]);
- ch[y][]=++tot; fa[tot]=y;
- key[tot]=has[tot]=c[]-'a'+;
- siz[tot]=;
- update(y); update(x);
- }
- else
- {
- ch[x][]=++tot; fa[tot]=x;
- key[tot]=has[tot]=c[]-'a'+;
- siz[tot]=;
- update(x);
- }
- len++;
- }
- }
- }
1014: [JSOI2008]火星人prefix
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 8125 Solved: 2572
[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
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
1、所有字符串自始至终都只有小写字母构成。
2、M<=150,000
3、字符串长度L自始至终都满足L<=100,000
4、询问操作的个数不超过10,000个。
对于第1,2个数据,字符串长度自始至终都不超过1,000
对于第3,4,5个数据,没有插入操作。
bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix的更多相关文章
- [BZOJ1014][JSOI2008]火星人prefix
[BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...
- BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*
BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...
- 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)
[JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...
- bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块
http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...
- bzoj千题计划192:bzoj1569: [JSOI2008]Blue Mary的职员分配
http://www.lydsy.com/JudgeOnline/problem.php?id=1569 dp[i][j][a][b] 表示i个职员,发广告状态为j,已有金钱a,声誉b的最少天数 j= ...
- bzoj千题计划219:bzoj1568: [JSOI2008]Blue Mary开公司
http://www.lydsy.com/JudgeOnline/problem.php?id=1568 写多了就觉着水了... #include<cstdio> #include< ...
- bzoj千题计划104:bzoj1013: [JSOI2008]球形空间产生器sphere
http://www.lydsy.com/JudgeOnline/problem.php?id=1013 设球心(x1,x2,x3……) 已知点的坐标为t[i][j] 那么 对于每个i满足 Σ (t[ ...
- bzoj千题计划196:bzoj4826: [Hnoi2017]影魔
http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...
- bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪
http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...
随机推荐
- 浅谈对IT的认识!
我是一个从农村出来的学生,家里的情况和大多数的农村同学是一样的,家里算不上有钱,父母供我读书,也已经是做到仁至义尽了. 我现在选了,一个和计算机有关的专业---计算机应用技术.就是希望毕业后,可以找到 ...
- ASP.NET中实现封装与策略模式
首先把运算方法封装起来,这样在网页界面中直接就可以调用了,不过是换张脸而已! using System; using System.Collections.Generic; using System. ...
- C++课程 second work _1025
传送门 Problem 题目不是特别难,只是跪在了最后一个测试点(已解决). 最后一个测试点= = 无效节点...无力ing
- Vue2.0组件之间通信(转载)
Vue中组件这个特性让不少前端er非常喜欢,我自己也是其中之一,它让前端的组件式开发更加合理和简单.笔者之前有写过一篇Vue2.0子父组件通信,这次我们就来聊一聊平级组件之间的通信. 首先我们先搭好开 ...
- [日常工作] 应用服务器上面应该尽量少开各种应用 --Chrome 内存泄露 让应用服务器非常缓慢
1. 前段时间修改 服务器的密码 导致应用程序的web site 启动有问题 ,打开chrome 查看了错误详细信息 但是忘记关了.. 今天反馈机器非常缓慢 简单看了下内存 吐血... 所以以后不能在 ...
- mac下快速安装gearman和php扩展
1.brew install gearman 用brew安装gearman 2.pecl install gearman 用pecl安装php的gearman扩展 3.ln -s /usr/local ...
- DataTable List 相互转换
This uses the FastMember's meta-programming API for maximum performance. If you want to restrict it ...
- File FileStream StreamReader StreamWriter C#
存在各种各样的IO设备,比如说文件File类(字符串文件和二进制文件),可以直接使用File类对文件进行读写操作. 这些各种IO的读取和写入是通过流的形式实现的,基类为Stream,针对各种不同的IO ...
- Python学习---字符串操作
### 截取字符串然后拼接 str = "Hello World!" str2 = str[:6] + "tyche !" print(str2) ===> ...
- Java中,一切皆是对象!为何数据类型中还分为:基本类型和对象?
Java中一切皆是对象!这句话没错,因为八种基本类型都有对应的包装类(int的包装类是Integer),包装类自然就是对象了. 基本类型一直都是Java语言的一部分,这主要是基于程序性能的考量, 基本 ...