博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【POJ3294】 Life Forms(SA)
阅读量:6986 次
发布时间:2019-06-27

本文共 2715 字,大约阅读时间需要 9 分钟。

...又是TLE,对于单组数据肯定TLE不了,问题是多组的时候就呵呵了...

按height分组去搞,然后判一下是否不属于同一个串...

const maxn=100419;var x,y,rank,sa,c,col,h,rec:array[0..maxn] of longint; pd:array[0..maxn] of boolean; s:ansistring; n,k,maxlen,cnta:longint;function max(x,y:longint):longint; begin if x>y then exit(x) else exit(y); end;function min(x,y:longint):longint; begin if x
x[sa[i-1]]) or (x[sa[i]+p]<>x[sa[i-1]+p]) then inc(tot); rank[sa[i]]:=tot; end; p:=p<<1; end; for i:= 1 to n do sa[rank[i]]:=i;end;procedure makeh;var i,p,j:longint;begin p:=0; h[1]:=0; for i:= 1 to n do begin p:=max(p-1,0); if rank[i]=1 then continue; j:=sa[rank[i]-1]; while (j+p<=n) and (i+p<=n) and (s[i+p]=s[j+p]) do inc(p); h[rank[i]]:=p; end;end;procedure init;var i,j,tot:longint; s1:ansistring;begin k:=n>>1; readln(s); tot:=1; maxlen:=length(s); for i:= 1 to maxlen do col[i]:=1; for i:= 2 to n do begin readln(s1); s:=s+'#'; maxlen:=max(length(s1),maxlen); inc(tot); for j:= length(s)+1 to length(s)+length(s1) do col[j]:=tot; s:=s+s1; end; n:=length(s); fillchar(c,sizeof(c),0); for i:= 1 to n do x[i]:=ord(s[i]); for i:= 1 to n do inc(c[x[i]]); for i:= 1 to 128 do inc(c[i],c[i-1]); for i:= 1 to n do begin sa[c[x[i]]]:=i; dec(c[x[i]]); end; tot:=1; rank[sa[1]]:=1; for i:= 2 to n do begin if x[sa[i]]<>x[sa[i-1]] then inc(tot); rank[sa[i]]:=tot; end; make; makeh;end;function check(x:longint):boolean;var i,j,tot,cnt:longint;begin fillchar(pd,sizeof(pd),false); pd[0]:=true; tot:=0; cnt:=0; if not pd[sa[1]] then inc(tot); pd[sa[1]]:=true; for i:= 2 to n do begin if h[i]
k then begin inc(cnt); rec[cnt]:=i-1; end; tot:=0; fillchar(pd,sizeof(pd),false); pd[0]:=true; end; if not pd[col[sa[i]]] then inc(tot); pd[col[sa[i]]]:=true; end; if tot>k then begin inc(cnt); rec[cnt]:=i-1; end; if cnt>0 then begin cnta:=cnt; exit(true); end; exit(false);end;procedure qsort(l,r:longint);var i,j,mid,tmp:longint;begin if l>r then exit; i:=l; j:=r; mid:=rec[l+random(r-l+1)]; repeat while rec[i]
mid do dec(j); if i<=j then begin swap(i,j); inc(i); dec(j); end; until i>j; qsort(i,r); qsort(l,j);end;procedure solve;var l,r,mid,tot,ans,i,j:longint;begin l:=1; r:=maxlen; ans:=0; while l<=r do begin mid:=(l+r)>>1; if check(mid) then begin ans:=mid; l:=mid+1; end else r:=mid-1; end; fillchar(pd,sizeof(pd),false); if ans=0 then begin writeln('?'); exit; end; qsort(1,cnta); for i:= 1 to cnta do writeln(copy(s,sa[rec[i]],ans));end;Begin readln(n); while n<>0 do begin init; solve; writeln; readln(n); end;End.

 

转载于:https://www.cnblogs.com/EC-Ecstasy/p/4175440.html

你可能感兴趣的文章
一个轻量级的数据驱动列表框架 YHListKit
查看>>
启动页
查看>>
微信红包的随机算法是怎样实现的?
查看>>
underscorejs源码学习系列(未完,更新中)
查看>>
Java 8 Lambda表达式一看就会
查看>>
抢火车票这个事吧,其实我也能做!(python黑科技)
查看>>
H5直播入门(理论篇)
查看>>
泰坦尼克生还预测:完整的机器学习项目(一)
查看>>
自定义CollectionView UICollectionViewLayout实现横向布局分页Emoji
查看>>
[Python3网络爬虫开发实战] 3-基本库的使用 1-使用urllib
查看>>
Session共享解决方案
查看>>
前端测试:Part II (单元测试)
查看>>
彻底搞懂Bean加载
查看>>
iOS之传值
查看>>
技术blog 迁移
查看>>
linux 定时器怎么用? crontab 基础
查看>>
React Native - Image
查看>>
Docker和宿主机操作系统文件目录互相隔离的实现原理
查看>>
小程序踩坑系列一
查看>>
探索webpack热更新对代码打包结果的影响(二)
查看>>