1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2918  Solved: 866
[Submit][Status]

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

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
数据规模:
对于100%的数据,满足:
1、 所有字符串自始至终都只有小写字母构成。
2、 M <= 150,000
3、 字符串长度L自始至终都满足L <= 100,000
4、 询问操作的个数不超过10,000个。

对于第1,2个数据,字符串长度自始至终都不超过1,000
对于第3,4,5个数据,没有插入操作。

HINT

 

Source

题解:

以前看着这题好麻烦,什么都不会的说。。。

现在看起来用splay似乎是显然的。。。

还是吐槽:

1.第一遍打错了好几个关键的地方竟然还能得70分。。。

2.为什么我的splay总是这么慢。。。

代码:

1.mine  光荣的TLE,不过在tyvj上AC的

  1. const maxn=+;
  2. p=;
  3. var i,n,m,x,t,y,z,l,r,rt,mid,tot:longint;
  4. ch:char;
  5. st:ansistring;
  6. s,v,w,fa,a:array[..maxn] of int64;
  7. c:array[..maxn,..] of longint;
  8. procedure pushup(x:longint);
  9. begin
  10. s[x]:=s[c[x,]]+s[c[x,]]+;
  11. w[x]:=(w[c[x,]]*a[s[c[x,]]+] mod p+
  12. v[x]*a[s[c[x,]]] mod p
  13. +w[c[x,]] mod p) mod p;
  14. end;
  15. procedure rotate(x:longint;var k:longint);
  16. var l,r,y,z:longint;
  17. begin
  18. y:=fa[x];z:=fa[y];
  19. l:=ord(c[y,]=x);r:=l xor ;
  20. if y=k then k:=x else c[z,ord(c[z,]=y)]:=x;
  21. fa[x]:=z;fa[y]:=x;fa[c[x,r]]:=y;
  22. c[y,l]:=c[x,r];c[x,r]:=y;
  23. pushup(y);pushup(x);
  24. end;
  25. procedure splay(x:longint;var k:longint);
  26. var y,z:longint;
  27. begin
  28. while x<>k do
  29. begin
  30. y:=fa[x];z:=fa[y];
  31. if y<>k then
  32. if (c[z,]=y) xor (c[y,]=x) then rotate(x,k)
  33. else rotate(y,k);
  34. rotate(x,k);
  35. end;
  36. end;
  37. function find(x,rank:longint):longint;
  38. var l,r:longint;
  39. begin
  40. l:=c[x,];r:=c[x,];
  41. if s[l]+=rank then exit(x)
  42. else if s[l]>=rank then exit(find(l,rank))
  43. else exit(find(r,rank-s[l]-));
  44. end;
  45. procedure del(k:longint);
  46. var x,y,z:longint;
  47. begin
  48. x:=find(rt,k-);y:=find(rt,k+);
  49. splay(x,rt);splay(y,c[x,]);
  50. z:=c[y,];fa[z]:=;s[z]:=;c[y,]:=;
  51. pushup(y);pushup(x);
  52. end;
  53.  
  54. procedure build(l,r,f:longint);
  55. var mid,now,last:longint;
  56. begin
  57. if l>r then exit;
  58. mid:=(l+r)>>;
  59. now:=mid;last:=f;
  60. fa[now]:=last;
  61. c[last,ord(mid>f)]:=now;
  62. if l=r then
  63. begin
  64. s[now]:=;w[now]:=v[now];
  65. exit;
  66. end;
  67. build(l,mid-,mid);build(mid+,r,mid);
  68. pushup(now);
  69. end;
  70.  
  71. procedure init;
  72. begin
  73. a[]:=;
  74. for i:= to maxn do a[i]:=(a[i-]*) mod p;
  75. readln(st);n:=length(st);
  76. for i:= to n+ do v[i]:=ord(st[i-])-ord('a')+;
  77. build(,n+,);rt:=(n+)>>;
  78. end;
  79. function check(l,r,len:longint):boolean;
  80. var x,y,xx,yy:longint;
  81. begin
  82. if len= then exit(true);
  83. x:=find(rt,l);y:=find(rt,l+len+);
  84. splay(x,rt);splay(y,c[x,]);
  85. xx:=w[c[y,]];
  86. x:=find(rt,r);y:=find(rt,r+len+);
  87. splay(x,rt);splay(y,c[x,]);
  88. yy:=w[c[y,]];
  89. if xx=yy then exit(true) else exit(false);
  90. end;
  91. function query(x,y:longint):longint;
  92. var l,r,mid:longint;
  93. begin
  94. if x>y then begin t:=x;x:=y;y:=t;end;
  95. if x=y then exit(tot-y-);
  96. if not(check(x,y,)) then exit();
  97. if check(x,y,tot-y-) then exit(tot-y-);
  98. l:=;r:=tot-y-;
  99. while l<r do
  100. begin
  101. mid:=(l+r)>>;
  102. if check(x,y,mid) then l:=mid+ else r:=mid;
  103. end;
  104. exit(l-);
  105. end;
  106. procedure main;
  107. begin
  108. readln(m);tot:=n+;
  109. for i:= to m do
  110. begin
  111. read(ch);
  112. case ch of
  113. 'R':begin
  114. read(x);read(ch);readln(ch);
  115. x:=find(rt,x+);
  116. splay(x,rt);
  117. v[x]:=ord(ch)-ord('a')+;
  118. pushup(x);
  119. end;
  120. 'I':begin
  121. read(z);read(ch);readln(ch);
  122. x:=find(rt,z+);y:=find(rt,z+);
  123. splay(x,rt);splay(y,c[x,]);
  124. inc(tot);c[y,]:=tot;fa[tot]:=y;s[tot]:=;
  125. v[tot]:=ord(ch)-ord('a')+;w[tot]:=v[tot];
  126. pushup(y);pushup(x);
  127. end;
  128. 'Q':begin
  129. readln(x,y);
  130. writeln(query(x,y));
  131. end;
  132. end;
  133. end;
  134. end;
  135. begin
  136. assign(input,'input.txt');assign(output,'output.txt');
  137. reset(input);rewrite(output);
  138. init;
  139. main;
  140. close(input);close(output);
  141. end.

2.在网上找了一份,模版和我的差不多  AC

  1. program hy_;
  2. const maxn=;
  3. var h:array[..] of int64;
  4. d:array[..] of int64;
  5. a,sum:array[..] of int64;
  6. fa:array[..] of longint;
  7. ll,rr:array[..] of longint;
  8. c:array[..,..] of longint;
  9. size,k,kk,root,n,m,all:longint;
  10. hh,ch:array[..] of char;
  11. //============================================================================
  12. procedure update(x:longint);
  13. var p,q:longint;
  14. begin
  15. sum[x]:=sum[c[x,]]+sum[c[x,]]+;
  16. p:=c[x,]; q:=c[x,];
  17. h[x]:=(h[p]+a[x]*d[sum[p]+]+h[q]*d[sum[p]+]) mod maxn; //更新hash值。
  18. end;
  19. //============================================================================
  20. procedure build(l,r:longint); //一开始用递归建树,防止树的深度太大。
  21. var mid,tmp:longint;
  22. begin
  23. mid:=(l+r) shr ;
  24. if mid>l then
  25. begin
  26. build(l,mid-);
  27. tmp:=(l+mid-) shr ;
  28. c[mid,]:=tmp; fa[tmp]:=mid;
  29. end;
  30. if mid<r then
  31. begin
  32. build(mid+,r);
  33. tmp:=(mid++r) shr ;
  34. c[mid,]:=tmp; fa[tmp]:=mid;
  35. end; update(mid);
  36. end;
  37. //============================================================================
  38. procedure init;
  39. var i,len:longint;
  40. st:ansistring;
  41. chh:char;
  42. begin
  43. readln(st); len:=length(st);
  44. for i:= to len+ do //在头尾预留两个单元,splay时就总能找到l-和r+。
  45. begin
  46. a[i]:=ord(st[i-])-ord('a');
  47. h[i]:=a[i];
  48. end; d[]:=;
  49. for i:= to do
  50. d[i]:=(d[i-]*) mod maxn; //预处理k的n次方(见hash函数)。
  51. build(,len+); root:=(len+) shr ;
  52. readln(m); size:=len+;
  53. for i:= to m do
  54. begin
  55. read(ch[i]);
  56. if ch[i]='Q' then
  57. begin
  58. inc(all); readln(ll[i],rr[i]);
  59. end else readln(ll[i],chh,hh[i]);
  60. end;
  61. end;
  62. //============================================================================
  63. function find(x:longint):longint;
  64. begin
  65. if sum[c[x,]]+=k then exit(x);
  66. if sum[c[x,]]<k- then
  67. begin
  68. dec(k,sum[c[x,]]+);
  69. find:=find(c[x,]);
  70. end else find:=find(c[x,]);
  71. end;
  72. //============================================================================
  73. procedure rotate(var root:longint; x:longint);
  74. var y,z,p,q:longint;
  75. begin
  76. y:=fa[x]; z:=fa[y];
  77. if c[y,]=x then p:= else p:=;
  78. q:=p xor ;
  79. if y=root then root:=x else
  80. if c[z,]=y then c[z,]:=x else c[z,]:=x;
  81. fa[x]:=z; fa[y]:=x; fa[c[x,q]]:=y;
  82. c[y,p]:=c[x,q]; c[x,q]:=y;
  83. update(y); update(x);
  84. end;
  85. //============================================================================
  86. procedure splay(var root:longint; x:longint);
  87. var y,z:longint;
  88. begin
  89. while x<>root do
  90. begin
  91. y:=fa[x]; z:=fa[y];
  92. if y<>root then
  93. if (c[y,]=x) xor (c[z,]=y) then
  94. rotate(root,x) else rotate(root,y);
  95. rotate(root,x);
  96. end;
  97. end;
  98. //============================================================================
  99. procedure insert(x:longint);
  100. begin
  101. if x= then
  102. begin
  103. inc(size); h[size]:=kk; sum[size]:=;
  104. a[size]:=h[size]; exit;
  105. end;
  106. if sum[c[x,]]>=k then
  107. begin
  108. insert(c[x,]);
  109. if c[x,]= then begin c[x,]:=size; fa[size]:=x; end;
  110. end else
  111. begin
  112. k:=k-sum[c[x,]]-; insert(c[x,]);
  113. if c[x,]= then begin c[x,]:=size; fa[size]:=x; end;
  114. end; update(x);
  115. end;
  116. //============================================================================
  117. procedure ask(x:longint);
  118. var xx,yy,l,r,ans,l1,l2,left1,left2:longint;
  119. hash1,hash2,s,t,mid,tt:longint;
  120. begin
  121. if rr[x]<ll[x] then
  122. begin
  123. tt:=rr[x]; rr[x]:=ll[x]; ll[x]:=tt;
  124. end;
  125. if ll[x]=rr[x] then
  126. begin writeln(size--ll[x]); exit; end;
  127. k:=ll[x]; l:=find(root); splay(root,l);
  128. k:=rr[x]; r:=find(root);
  129. k:=ll[x]+; xx:=find(root); k:=rr[x]+; yy:=find(root);
  130. if a[xx]<>a[yy] then
  131. begin writeln(''); exit; end;
  132. s:=; t:=size-rr[x]-; ans:=;
  133. k:=ll[x]+t+; l1:=find(root);
  134. k:=rr[x]+t+; l2:=find(root);
  135. splay(c[root,],l1); hash1:=h[c[l1,]];
  136. splay(c[root,],r); splay(c[r,],l2); hash2:=h[c[l2,]];
  137. if hash1=hash2 then begin writeln(t); exit; end; //这里特判了最长长度的串。。有的数据太猥琐。。。
  138. dec(t);
  139. repeat
  140. mid:=(s+t) shr ;
  141. k:=ll[x]+mid+; l1:=find(root);
  142. k:=rr[x]+mid+; l2:=find(root);
  143. splay(c[root,],l1); hash1:=h[c[l1,]];
  144. splay(c[root,],r); splay(c[r,],l2); hash2:=h[c[l2,]];
  145. if hash1=hash2 then
  146. begin s:=mid+; ans:=mid; end else t:=mid-;
  147. until s>t; writeln(ans);
  148. end;
  149. //============================================================================
  150. procedure rep(x:longint);
  151. var z,p,q:longint;
  152. begin
  153. k:=ll[x]+; z:=find(root); splay(root,z);
  154. a[z]:=ord(hh[x])-ord('a'); p:=c[root,]; q:=c[root,];
  155. h[z]:=(h[p]+a[z]*d[sum[p]+]+h[q]*d[sum[p]+]);
  156. end;
  157. //============================================================================
  158. procedure ins(x:longint);
  159. begin
  160. k:=ll[x]+; kk:=ord(hh[x])-ord('a'); insert(root);
  161. splay(root,size);
  162. end;
  163. //============================================================================
  164. procedure work;
  165. var i:longint;
  166. begin
  167. for i:= to m do
  168. begin
  169. if ch[i]='Q' then
  170. begin
  171. ask(i); dec(all);
  172. if all= then halt;
  173. end else if ch[i]='R' then rep(i) else ins(i);
  174. end;
  175. end;
  176. //============================================================================
  177. begin
  178. assign(input,'input.txt');assign(output,'output2.txt');
  179. reset(input);rewrite(output);
  180. init;
  181. work;
  182. close(input);close(output);
  183. end.

JSOI2008 火星人prefix的更多相关文章

  1. 1014: [JSOI2008]火星人prefix

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Description 火星人最近研究了一种操作:求一个字串两个后缀 ...

  2. [BZOJ1014][JSOI2008]火星人prefix

    [BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...

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

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

  4. 【bzoj1014】[JSOI2008]火星人prefix

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

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

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

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

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

  7. 求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix

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

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

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

  9. 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)

    [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...

随机推荐

  1. MathType支持64位 WIN 7Office 2013(完美解决)(转载)

    经过几次尝试解决了,方法如下: 1. 安装MathType 6.8 (别的版本不知是否适用,本人安装的是该版本) 2. 将以下两个文件拷贝出来 C:\Program Files (x86)\MathT ...

  2. iOS imagePicker使用方法,方便使用!三步轻松搞定!

    自己总结的修改头像的方法,只为方便自己查询使用!转发 步骤:1.遵守代理协议 <UIImagePickerControllerDelegate,UINavigationControllerDel ...

  3. iOS zipzap读取压缩文件

    最近在公司遇到一项需求,在不解压zip文件的情况下读取其中的文件,因为之前使用的ziparchive不能满足现在的需求,所以在网上一阵狂搜,终于找到了zipzap,实话说还真的难找. 之前读取本地zi ...

  4. cordova安装中的坑

    1.安装android环境直接略过! 2.安装node.js直接略过! 3.安装cordova npm install -g cordova npm uninstall cordova  -g(这条是 ...

  5. 这样写JS的方式对吗?

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. LA 3708 Graveyard(推理 参考系 中位数)

    Graveyard Programming contests became so popular in the year 2397 that the governor of New Earck -- ...

  7. HttpHandler与HttpModule及实现文件下载

    HttpHandler:处理请求(Request)的信息和发送响应(Response).HttpModule:通过Http Module向Http请求输出流中写入文字,httpmodule先执行 它们 ...

  8. MySQL常见错误类型

    MySQL常见错误类型:1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库失败1009:不能删除数据库文件导致删除数据库失败1010: ...

  9. mini2440移植uboot-2008.10 (一)

    今天是我申请此博客的第一天,从现在开始养成每次学完新东西,都要写博客进行总结的习惯!!!Come on! 首先就从这两天学的uboot开始吧,这两天学这个东西郁闷死了,不知道该怎么去学,尤其是到nan ...

  10. jQuery如何检查某个元素在网页上是否存在

    $("ID")获取的永远是对象,即使网页上没有此元素.因此当要用jQuery检查某个元素在网页上是否存在时,不能使用以下代码: if($("#ID")){ // ...