http://www.lydsy.com/JudgeOnline/problem.php?id=1014

两个后缀的最长公共前缀:二分+hash

带修改带插入:splay维护

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4.  
  5. #define L 100001
  6.  
  7. typedef unsigned long long ULL;
  8.  
  9. using namespace std;
  10.  
  11. char s[L+];
  12.  
  13. int tot,root;
  14. int fa[L+],ch[L+][],siz[L+],key[L+];
  15. ULL has[L+];
  16.  
  17. ULL bit[L+];
  18.  
  19. int len;
  20.  
  21. void read(int &x)
  22. {
  23. x=; char c=getchar();
  24. while(!isdigit(c)) c=getchar();
  25. while(isdigit(c)) { x=x*+c-''; c=getchar();}
  26. }
  27.  
  28. void update(int x)
  29. {
  30. siz[x]=siz[ch[x][]]+siz[ch[x][]]+;
  31. has[x]=has[ch[x][]]*bit[siz[ch[x][]]+]+key[x]*bit[siz[ch[x][]]]+has[ch[x][]];
  32. }
  33.  
  34. void build(int l,int r,int f)
  35. {
  36. if(l>r) return;
  37. int mid=l+r>>;
  38. ch[f][mid>f]=mid;
  39. fa[mid]=f;
  40. siz[mid]=; key[mid]=s[mid]-'a'+;
  41. build(l,mid-,mid);
  42. build(mid+,r,mid);
  43. update(mid);
  44. }
  45.  
  46. int find(int x)
  47. {
  48. int now=root;
  49. while()
  50. {
  51. if(siz[ch[now][]]>=x) now=ch[now][];
  52. else
  53. {
  54. x-=siz[ch[now][]];
  55. if(x==) return now;
  56. x--; now=ch[now][];
  57. }
  58. }
  59. }
  60.  
  61. int findsuf(int x)
  62. {
  63. int now=ch[x][];
  64. while(ch[now][]) now=ch[now][];
  65. return now;
  66. }
  67.  
  68. void rotate(int x,int &goal)
  69. {
  70. int y=fa[x],z=fa[y],k=ch[y][]==x;
  71. if(y!=goal) ch[z][ch[z][]==y]=x;
  72. else goal=x;
  73. ch[y][k]=ch[x][k^]; ch[x][k^]=y;
  74. fa[x]=z; fa[y]=x; fa[ch[y][k]]=y;
  75. update(y);
  76. }
  77.  
  78. bool getson(int x)
  79. {
  80. return ch[fa[x]][]==x;
  81. }
  82.  
  83. void splay(int x,int &goal)
  84. {
  85. int y;
  86. while(x!=goal)
  87. {
  88. y=fa[x];
  89. if(y==goal) rotate(x,goal);
  90. else
  91. {
  92. if(getson(x)^getson(y)) rotate(x,goal);
  93. else rotate(y,goal);
  94. rotate(x,goal);
  95. }
  96. update(x);
  97. }
  98.  
  99. }
  100.  
  101. ULL query(int l,int r)
  102. {
  103. if(r==len+)
  104. {
  105. int t1=find(l-);
  106. splay(t1,root);
  107. return has[ch[root][]];
  108. }
  109. int tl=find(l-),tr=find(r+);
  110. splay(tl,root);
  111. splay(tr,ch[root][]);
  112. return has[ch[tr][]];
  113. }
  114.  
  115. int main()
  116. {
  117. //freopen("bzoj_1014.in","r",stdin);
  118. //freopen("bzoj_1014.out","w",stdout);
  119. scanf("%s",s+); len=strlen(s+);
  120. int m; read(m);
  121. bit[]=; for(int i=;i<L;++i) bit[i]=bit[i-]*;
  122. build(,len+,);
  123. root=len+>>;
  124. char c[]; int x,y;
  125. int l,r,mid,ans;
  126. tot=len+;
  127. while(m--)
  128. {
  129. scanf("%s",c);
  130. if(c[]=='Q')
  131. {
  132. read(x); read(y);
  133. x++; y++;
  134. ans=; l=; r=min(len+-x+,len+-y+);
  135. while(l<=r)
  136. {
  137. mid=l+r>>;
  138. if(query(x,x+mid-)!=query(y,y+mid-)) r=mid-;
  139. else l=mid+,ans=mid;
  140. }
  141. cout<<ans<<'\n';
  142. }
  143. else if(c[]=='R')
  144. {
  145. read(x); scanf("%s",c);
  146. x++;
  147. x=find(x);
  148. splay(x,root);
  149. key[x]=c[]-'a'+;
  150. }
  151. else
  152. {
  153. read(x); scanf("%s",c);
  154. x++;
  155. x=find(x); splay(x,root);
  156. y=findsuf(x);
  157. if(y)
  158. {
  159. splay(y,ch[x][]);
  160. ch[y][]=++tot; fa[tot]=y;
  161. key[tot]=has[tot]=c[]-'a'+;
  162. siz[tot]=;
  163. update(y); update(x);
  164. }
  165. else
  166. {
  167. ch[x][]=++tot; fa[tot]=x;
  168. key[tot]=has[tot]=c[]-'a'+;
  169. siz[tot]=;
  170. update(x);
  171. }
  172. len++;
  173. }
  174. }
  175. }

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

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

bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix的更多相关文章

  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千题计划300:bzoj4823: [Cqoi2017]老C的方块

    http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...

  5. 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= ...

  6. bzoj千题计划219:bzoj1568: [JSOI2008]Blue Mary开公司

    http://www.lydsy.com/JudgeOnline/problem.php?id=1568 写多了就觉着水了... #include<cstdio> #include< ...

  7. bzoj千题计划104:bzoj1013: [JSOI2008]球形空间产生器sphere

    http://www.lydsy.com/JudgeOnline/problem.php?id=1013 设球心(x1,x2,x3……) 已知点的坐标为t[i][j] 那么 对于每个i满足 Σ (t[ ...

  8. bzoj千题计划196:bzoj4826: [Hnoi2017]影魔

    http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...

  9. bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪

    http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...

随机推荐

  1. 浅谈对IT的认识!

    我是一个从农村出来的学生,家里的情况和大多数的农村同学是一样的,家里算不上有钱,父母供我读书,也已经是做到仁至义尽了. 我现在选了,一个和计算机有关的专业---计算机应用技术.就是希望毕业后,可以找到 ...

  2. ASP.NET中实现封装与策略模式

    首先把运算方法封装起来,这样在网页界面中直接就可以调用了,不过是换张脸而已! using System; using System.Collections.Generic; using System. ...

  3. C++课程 second work _1025

    传送门 Problem 题目不是特别难,只是跪在了最后一个测试点(已解决). 最后一个测试点= = 无效节点...无力ing

  4. Vue2.0组件之间通信(转载)

    Vue中组件这个特性让不少前端er非常喜欢,我自己也是其中之一,它让前端的组件式开发更加合理和简单.笔者之前有写过一篇Vue2.0子父组件通信,这次我们就来聊一聊平级组件之间的通信. 首先我们先搭好开 ...

  5. [日常工作] 应用服务器上面应该尽量少开各种应用 --Chrome 内存泄露 让应用服务器非常缓慢

    1. 前段时间修改 服务器的密码 导致应用程序的web site 启动有问题 ,打开chrome 查看了错误详细信息 但是忘记关了.. 今天反馈机器非常缓慢 简单看了下内存 吐血... 所以以后不能在 ...

  6. mac下快速安装gearman和php扩展

    1.brew install gearman 用brew安装gearman 2.pecl install gearman 用pecl安装php的gearman扩展 3.ln -s /usr/local ...

  7. DataTable List 相互转换

    This uses the FastMember's meta-programming API for maximum performance. If you want to restrict it ...

  8. File FileStream StreamReader StreamWriter C#

    存在各种各样的IO设备,比如说文件File类(字符串文件和二进制文件),可以直接使用File类对文件进行读写操作. 这些各种IO的读取和写入是通过流的形式实现的,基类为Stream,针对各种不同的IO ...

  9. Python学习---字符串操作

    ### 截取字符串然后拼接 str = "Hello World!" str2 = str[:6] + "tyche !" print(str2) ===> ...

  10. Java中,一切皆是对象!为何数据类型中还分为:基本类型和对象?

    Java中一切皆是对象!这句话没错,因为八种基本类型都有对应的包装类(int的包装类是Integer),包装类自然就是对象了. 基本类型一直都是Java语言的一部分,这主要是基于程序性能的考量, 基本 ...