JSOI2008 火星人prefix

时间:2014-08-06 01:50:00   收藏:0   阅读:317

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的

bubuko.com,布布扣
  1 const maxn=100000+10;
  2       p=9875321;
  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[0..maxn] of int64;
  7     c:array[0..maxn,0..1] of longint;
  8 procedure pushup(x:longint);
  9  begin
 10    s[x]:=s[c[x,0]]+s[c[x,1]]+1;
 11    w[x]:=(w[c[x,0]]*a[s[c[x,1]]+1] mod p+
 12          v[x]*a[s[c[x,1]]] mod p
 13          +w[c[x,1]] 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,1]=x);r:=l xor 1;
 20    if y=k then k:=x else c[z,ord(c[z,1]=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,0]=y) xor (c[y,0]=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,0];r:=c[x,1];
 41    if s[l]+1=rank then exit(x)
 42    else if s[l]>=rank then exit(find(l,rank))
 43    else exit(find(r,rank-s[l]-1));
 44  end;
 45 procedure del(k:longint);
 46  var x,y,z:longint;
 47  begin
 48    x:=find(rt,k-1);y:=find(rt,k+1);
 49    splay(x,rt);splay(y,c[x,1]);
 50    z:=c[y,0];fa[z]:=0;s[z]:=0;c[y,0]:=0;
 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)>>1;
 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]:=1;w[now]:=v[now];
 65       exit;
 66      end;
 67    build(l,mid-1,mid);build(mid+1,r,mid);
 68    pushup(now);
 69  end;
 70 
 71 procedure init;
 72  begin
 73    a[0]:=1;
 74    for i:=1 to maxn do a[i]:=(a[i-1]*31) mod p;
 75    readln(st);n:=length(st);
 76     for i:=2 to n+1 do v[i]:=ord(st[i-1])-ord(a)+1;
 77    build(1,n+2,0);rt:=(n+3)>>1;
 78  end;
 79 function check(l,r,len:longint):boolean;
 80  var x,y,xx,yy:longint;
 81  begin
 82   if len=0 then exit(true);
 83   x:=find(rt,l);y:=find(rt,l+len+1);
 84   splay(x,rt);splay(y,c[x,1]);
 85   xx:=w[c[y,0]];
 86   x:=find(rt,r);y:=find(rt,r+len+1);
 87   splay(x,rt);splay(y,c[x,1]);
 88   yy:=w[c[y,0]];
 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-1);
 96   if not(check(x,y,1)) then exit(0);
 97   if check(x,y,tot-y-1) then exit(tot-y-1);
 98   l:=2;r:=tot-y-1;
 99   while l<r do
100       begin
101       mid:=(l+r)>>1;
102       if check(x,y,mid) then l:=mid+1 else r:=mid;
103       end;
104   exit(l-1);
105   end;
106 procedure main;
107  begin
108   readln(m);tot:=n+2;
109   for i:=1 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+1);
116           splay(x,rt);
117           v[x]:=ord(ch)-ord(a)+1;
118           pushup(x);
119           end;
120       I:begin
121           read(z);read(ch);readln(ch);
122           x:=find(rt,z+1);y:=find(rt,z+2);
123           splay(x,rt);splay(y,c[x,1]);
124           inc(tot);c[y,0]:=tot;fa[tot]:=y;s[tot]:=1;
125           v[tot]:=ord(ch)-ord(a)+1;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.
142           
View Code

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

bubuko.com,布布扣
  1 program hy_1014;
  2 const maxn=9875321;
  3 var h:array[0..100010] of int64;
  4     d:array[1..100010] of int64;
  5     a,sum:array[0..100010] of int64;
  6     fa:array[0..100010] of longint;
  7     ll,rr:array[1..150010] of longint;
  8     c:array[0..100010,0..1] of longint;
  9     size,k,kk,root,n,m,all:longint;
 10     hh,ch:array[1..150010] of char;
 11 //============================================================================
 12 procedure update(x:longint);
 13 var p,q:longint;
 14 begin
 15   sum[x]:=sum[c[x,0]]+sum[c[x,1]]+1;
 16   p:=c[x,0]; q:=c[x,1];
 17   h[x]:=(h[p]+a[x]*d[sum[p]+1]+h[q]*d[sum[p]+2]) mod maxn;   //更新hash值。
 18 end;
 19 //============================================================================
 20 procedure build(l,r:longint);   //一开始用递归建树,防止树的深度太大。
 21 var mid,tmp:longint;
 22 begin
 23   mid:=(l+r) shr 1;
 24   if mid>l then
 25   begin
 26     build(l,mid-1);
 27     tmp:=(l+mid-1) shr 1;
 28     c[mid,0]:=tmp; fa[tmp]:=mid;
 29   end;
 30   if mid<r then
 31   begin
 32     build(mid+1,r);
 33     tmp:=(mid+1+r) shr 1;
 34     c[mid,1]:=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:=2 to len+1 do  //在头尾预留两个单元,splay时就总能找到l-1和r+1 45   begin
 46     a[i]:=ord(st[i-1])-ord(a);
 47     h[i]:=a[i];
 48   end; d[1]:=1;
 49   for i:=2 to 100010 do
 50     d[i]:=(d[i-1]*27) mod maxn;   //预处理k的n次方(见hash函数)。
 51   build(1,len+2); root:=(len+3) shr 1;
 52   readln(m); size:=len+2;
 53   for i:=1 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,0]]+1=k then exit(x);
 66   if sum[c[x,0]]<k-1 then
 67   begin
 68     dec(k,sum[c[x,0]]+1);
 69     find:=find(c[x,1]);
 70   end else find:=find(c[x,0]);
 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,0]=x then p:=0 else p:=1;
 78   q:=p xor 1;
 79   if y=root then root:=x else
 80     if c[z,0]=y then c[z,0]:=x else c[z,1]:=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,0]=x) xor (c[z,0]=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=0 then
102   begin
103     inc(size); h[size]:=kk; sum[size]:=1;
104     a[size]:=h[size]; exit;
105   end;
106   if sum[c[x,0]]>=k then
107   begin
108     insert(c[x,0]);
109     if c[x,0]=0 then begin c[x,0]:=size; fa[size]:=x; end;
110   end else
111   begin
112     k:=k-sum[c[x,0]]-1; insert(c[x,1]);
113     if c[x,1]=0 then begin c[x,1]:=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-1-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]+1; xx:=find(root); k:=rr[x]+1; yy:=find(root);
130   if a[xx]<>a[yy] then
131   begin writeln(0); exit; end;
132   s:=1; t:=size-rr[x]-1; ans:=0;
133     k:=ll[x]+t+1; l1:=find(root);
134     k:=rr[x]+t+1; l2:=find(root);
135     splay(c[root,1],l1); hash1:=h[c[l1,0]];
136     splay(c[root,1],r); splay(c[r,1],l2); hash2:=h[c[l2,0]];
137     if hash1=hash2 then begin writeln(t); exit; end; //这里特判了最长长度的串。。有的数据太猥琐。。。
138   dec(t);
139   repeat
140     mid:=(s+t) shr 1;
141     k:=ll[x]+mid+1; l1:=find(root);
142     k:=rr[x]+mid+1; l2:=find(root);
143     splay(c[root,1],l1); hash1:=h[c[l1,0]];
144     splay(c[root,1],r); splay(c[r,1],l2); hash2:=h[c[l2,0]];
145     if hash1=hash2 then
146     begin s:=mid+1; ans:=mid; end else t:=mid-1;
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]+1; z:=find(root); splay(root,z);
154   a[z]:=ord(hh[x])-ord(a); p:=c[root,0]; q:=c[root,1];
155   h[z]:=(h[p]+a[z]*d[sum[p]+1]+h[q]*d[sum[p]+2]);
156 end;
157 //============================================================================
158 procedure ins(x:longint);
159 begin
160   k:=ll[x]+1; 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:=1 to m do
168   begin
169     if ch[i]=Q then
170     begin
171       ask(i); dec(all);
172       if all=0 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.    
View Code

 

JSOI2008 火星人prefix,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!