【BZOJ1014】【JSOI2008】火星人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】

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】

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


【Solution】

  Splay维护hash值,比较时二分判断哈希值是否相同即可。还有注意类型溢出。
  (程序跑得好慢,卡时过了qwq)
  
  代码如下:

/**************************************************************
    Problem: 1014
    User: llgyc
    Language: Pascal
    Result: Accepted
    Time:10820 ms
    Memory:4820 kb
****************************************************************/

const maxn = 150005; mo = 998244353;
var rt,sz:longint;
    size,v,h,base:array[0..maxn] of longint;
    lc,rc,fa:array[0..maxn] of longint;
    ch:array[0..maxn] of char;
function min(x,y:longint):longint; inline;
  begin if x<y then exit(x) else exit(y); end;
procedure update(x:longint); inline;
  var l,r:longint;
  begin
    l:=lc[x]; r:=rc[x];
    size[x]:=size[l]+size[r]+1;
    h[x]:=longint((int64(h[l])
                  +int64(v[x])*int64(base[size[l]])
                  +int64(h[r])*int64(base[size[l]+1]))
                  mod mo);
  end;
procedure build(x,l,r,f:longint); inline;
  begin
    if l>r then exit;
    if l=r then begin
      v[x]:=ord(ch[l])-ord('a')+1; h[x]:=v[x];
      size[x]:=1; fa[x]:=f;
      if l<f then lc[f]:=l else rc[f]:=l;
      exit;
    end;
    build((l+x-1)>>1,l,x-1,x); build((x+1+r)>>1,x+1,r,x);
    v[x]:=ord(ch[x])-ord('a')+1; fa[x]:=f; update(x);
    if x<f then lc[f]:=x else rc[f]:=x;
  end;
procedure init(); inline;
  var i,n:longint;
      s:ansistring;
  begin
    readln(s); n:=length(s); for i:=2 to n+1 do ch[i]:=s[i-1];
    base[0]:=1; for i:=1 to maxn do base[i]:=longint(int64(base[i-1])*27 mod mo);
    h[0]:=0; size[0]:=0; fa[0]:=0;
    build((n+3)>>1,1,n+2,0); sz:=n+2; rt:=(n+3)>>1;
  end;
procedure rotate(x:longint); inline;
  var y,z,b:longint;
  begin
    y:=fa[x]; z:=fa[y]; if x=lc[y] then b:=rc[x] else b:=lc[x];
    fa[x]:=z; fa[y]:=x;
    if b<>0 then fa[b]:=y;
    if z<>0
      then if lc[z]=y then lc[z]:=x else rc[z]:=x;
    if x=lc[y] then begin
      rc[x]:=y; lc[y]:=b;
    end
    else begin
      lc[x]:=y; rc[y]:=b;
    end;
    update(y); update(x);
  end;
procedure splay(x,target:longint); inline;
  begin
    while fa[x]<>target do begin
      if fa[fa[x]]<>target then begin
        if ((lc[fa[fa[x]]]=fa[x]) = (lc[fa[x]]=x))
          then rotate(fa[x])
          else rotate(x);
      end;
      rotate(x);
    end;
    if target=0 then rt:=x;
  end;
function find(x,rk:longint):longint; inline;
  var l,r:longint;
  begin
    l:=lc[x]; r:=rc[x];
    if size[l]+1=rk then exit(x);
    if size[l]>=rk then exit(find(l,rk));
    exit(find(r,rk-size[l]-1));
  end;
function query(x,val:longint):longint; inline;
  var a,b,c:longint;
  begin
    a:=find(rt,x); b:=find(rt,x+val+1);
    splay(a,0); splay(b,a); c:=lc[b];
    exit(h[c]);
  end;
function solve(x,y:longint):longint; inline;
  var l,r,mid,ans:longint;
  begin
    l:=1; r:=min(sz-x,sz-y)-1; ans:=0;
    while (l<=r) do begin
      mid:=(l+r) div 2;
      if query(x,mid)=query(y,mid)
        then begin
          l:=mid+1; ans:=mid;
        end
        else r:=mid-1;
    end;
    exit(ans);
  end;
procedure insert(x,val:longint); inline;
  var a,b,c:longint;
  begin
    a:=find(rt,x+1); b:=find(rt,x+2);
    splay(a,0); splay(b,a);
    inc(sz); c:=sz; lc[b]:=c; fa[c]:=b; v[c]:=val;
    update(c); update(b); update(a);
  end;
procedure main(); inline;
  var m,x,y,i:longint;
      c,d:char;
  begin
    readln(m);
    while (m>0) do begin
      dec(m); read(c); read(x);
      case c of
        'Q' : begin
                readln(y); writeln(solve(x,y));
              end;
        'R' : begin
                readln(d,d); x:=find(rt,x+1); splay(x,0);
                v[rt]:=ord(d)-ord('a')+1; update(rt);
              end;
        'I' : begin
                readln(d,d); insert(x,ord(d)-ord('a')+1);
              end;
      end;
    end;
  end;
begin
  init();
  main();
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值