后缀数组实现
来源:互联网 发布:安卓编程 编辑:程序博客网 时间:2024/06/08 06:47
先贴这里,免得忘了
program sa;type int=longint;var i,j,k,m,n,l,max:int; a,b,c,d,x,r:array[0..1000000]of int; ch:char;procedure main;begin l:=1; repeat for i:=0 to max do begin b[i]:=0;c[i]:=0;end; for i:=1 to n do begin inc(b[a[i]]);inc(c[a[i+l]]);end; for i:=1 to max do begin inc(b[i],b[i-1]);inc(c[i],c[i-1]);end; for i:=1 to n do begin d[c[a[i+l]]]:=i;dec(c[a[i+l]]);end; for i:=n downto 1 do begin k:=d[i];r[b[a[k]]]:=d[i];dec(b[a[k]]);end; max:=0; for i:=1 to n do begin if(a[r[i-1]]<>a[r[i]])or(a[r[i-1]+l]<>a[r[i]+l])then inc(max); x[r[i]]:=max; end; for i:=1 to n do a[i]:=x[i]; l:=l<<1; until max=n;end;begin assign(input,'sa.in');reset(input); assign(output,'sa.out');rewrite(output); while not eoln do begin inc(n);read(ch);a[n]:=ord(ch); if a[n]>max then max:=a[n]; end; main; for i:=1 to n do writeln(a[i]); close(input);close(output);end.
如果使用指针实现X,A的功能就可以少一个循环,应该会更快一点
最长公共子串:
program sa;type int=longint;var l,i,j,k,m,n,max,ans:int; a,b,c,d,r,x,h,st:array[0..500000]of int; ch:char;procedure main;begin l:=1;max:=200; repeat for i:=0 to max do begin c[i]:=0;b[i]:=0;end; for i:=1 to n do begin inc(b[a[i]]);inc(c[a[i+l]]);end; for i:=1 to max do begin inc(b[i],b[i-1]);inc(c[i],c[i-1]);end; for i:=1 to n do begin d[c[a[i+l]]]:=i;dec(c[a[i+l]]);end; for i:=n downto 1 do begin r[b[a[d[i]]]]:=d[i];dec(b[a[d[i]]]);end; max:=0; for i:=1 to n do begin if(a[r[i]]<>a[r[i-1]])or(a[r[i]+l]<>a[r[i-1]+l])then inc(max); x[r[i]]:=max; end; for i:=1 to n do a[i]:=x[i]; l:=l<<1; until max=n; for i:=1 to n do b[a[i]]:=i; for i:=1 to n do begin if h[i-1]>0 then h[i]:=h[i-1]-1; while st[b[a[i]-1]+h[i]]=st[i+h[i]] do inc(h[i]); end; for i:=1 to n do x[a[i]]:=h[i]; for i:=2 to n do if((b[i]<m)xor(b[i-1]<m))and(ans<x[i]) then ans:=x[i];end;//a->rank,b->sa,h->h,x->heightbegin assign(input,'data.txt');reset(input); assign(output,'ans.txt');rewrite(output); while not eoln do begin read(ch);inc(n);st[n]:=ord(ch);end; m:=n+1;readln; inc(n);st[n]:=200; while not eoln do begin read(ch);inc(n);st[n]:=ord(ch);end; a:=st;main; write(ans); close(input);close(output);end.
- 后缀数组实现
- 后缀数组的实现
- 后缀数组的实现
- 后缀数组--倍增法实现
- 倍增算法实现后缀数组
- 后缀数组:原理和实现
- 后缀数组的Java实现
- 最长公共子串--后缀数组实现
- 构造后缀数组的DC3算法实现
- 后缀数组的实现和字符串匹配
- 后缀数组的倍增法实现
- 倍增算法实现后缀数组的构造
- 后缀树/后缀数组
- 后缀树 后缀数组
- 【后缀数组】后缀排序
- 后缀数组
- 后缀数组
- 后缀数组
- 转帖yum和apt-get用法及区别,总结的不错
- c++builder Debugger Debugger Exception Notification Access数据库有空字段错误
- SqlServer !=与<>
- C#集合详解
- Google Play开发者账号注册教程(new)
- 后缀数组实现
- 3D图形学专业术语
- loaded the nib but view outlet was not set
- oracle常用服务含义
- friends 103 The One With the Thumb
- 找出唯一的满足a + b + c = 1000的毕达哥拉斯三元组{a, b, c}。
- QTP的描述性编程与正则表达式
- Unity学习笔记2:物理引擎和碰撞
- (x&y)+((x^y)>>1)(面试宝典)