【POJ3294】 Life Forms(SA)
...又是TLE,对于单组数据肯定TLE不了,问题是多组的时候就呵呵了...
按height分组去搞,然后判一下是否不属于同一个串...
const maxn=;
var
x,y,rank,sa,c,col,h,rec:array[..maxn] of longint;
pd:array[..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<y then exit(x) else exit(y); end;
procedure swap(var x,y:longint);var tmp:longint; begin tmp:=x;x:=y;y:=tmp; end; procedure make;
var i,p,tot:longint;
begin
p:=;
while p<n do
begin
fillchar(c,sizeof(c),);
for i:= to n-p do y[i]:=rank[i+p];
for i:= n-p+ to n do y[i]:=;
for i:= to n do inc(c[y[i]]);
for i:= to n do inc(c[i],c[i-]);
for i:= to n do
begin
sa[c[y[i]]]:=i;
dec(c[y[i]]);
end;
fillchar(c,sizeof(c),);
for i:= to n do x[i]:=rank[i];
for i:= to n do inc(c[x[i]]);
for i:= to n do inc(c[i],c[i-]);
for i:= n downto do
begin
y[sa[i]]:=c[x[sa[i]]];
dec(c[x[sa[i]]]);
end;
for i:= to n do sa[y[i]]:=i;
tot:=;
rank[sa[]]:=;
for i:= to n do
begin
if (x[sa[i]]<>x[sa[i-]]) or (x[sa[i]+p]<>x[sa[i-]+p]) then inc(tot);
rank[sa[i]]:=tot;
end;
p:=p<<;
end;
for i:= to n do sa[rank[i]]:=i;
end; procedure makeh;
var i,p,j:longint;
begin
p:=; h[]:=;
for i:= to n do
begin
p:=max(p-,);
if rank[i]= then continue;
j:=sa[rank[i]-];
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>>;
readln(s);
tot:=;
maxlen:=length(s);
for i:= to maxlen do col[i]:=;
for i:= to n do
begin
readln(s1);
s:=s+'#';
maxlen:=max(length(s1),maxlen);
inc(tot);
for j:= length(s)+ to length(s)+length(s1) do col[j]:=tot;
s:=s+s1;
end;
n:=length(s);
fillchar(c,sizeof(c),);
for i:= to n do x[i]:=ord(s[i]);
for i:= to n do inc(c[x[i]]);
for i:= to do inc(c[i],c[i-]);
for i:= to n do
begin
sa[c[x[i]]]:=i;
dec(c[x[i]]);
end;
tot:=;
rank[sa[]]:=;
for i:= to n do
begin
if x[sa[i]]<>x[sa[i-]] 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[]:=true; tot:=; cnt:=;
if not pd[sa[]] then inc(tot);
pd[sa[]]:=true;
for i:= to n do
begin
if h[i]<x then
begin
if tot>k then
begin
inc(cnt);
rec[cnt]:=i-;
end;
tot:=;
fillchar(pd,sizeof(pd),false);
pd[]:=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-;
end;
if cnt> 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+)];
repeat
while rec[i]<mid do inc(i);
while rec[j]>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:=; r:=maxlen; ans:=;
while l<=r do
begin
mid:=(l+r)>>;
if check(mid) then
begin
ans:=mid;
l:=mid+;
end
else r:=mid-;
end;
fillchar(pd,sizeof(pd),false);
if ans= then
begin
writeln('?');
exit;
end;
qsort(,cnta);
for i:= to cnta do writeln(copy(s,sa[rec[i]],ans));
end; Begin
readln(n);
while n<> do
begin
init;
solve;
writeln;
readln(n);
end;
End.
【POJ3294】 Life Forms(SA)的更多相关文章
- 【POJ3294】 Life Forms (后缀数组+二分)
Life Forms Description You may have wondered why most extraterrestrial life forms resemble humans, d ...
- 【poj3294】 Life Forms
http://poj.org/problem?id=3294 (题目链接) 题意 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. Solution 后缀数组论文题.. 将 n 个字符串 ...
- 【POJ3294】Life Forms(后缀数组,二分)
题意: n<=100 len[i]<=1000 思路:这是一道论文题 ..]of longint; ch:..]of ansistring; n,n1,l,r,mid,last,i,j,m ...
- 【转】Xamarin Forms 介绍
特此声明,本篇博文转自:http://blog.csdn.net/kinfey/article/details/29621381 什么是 Xamarin Forms ? Xamarin Forms 是 ...
- 【转】SQL2008的sa账户被禁用,其他账户无法连接的解决方法
或者你还有其它的sysadmin权限的账号,你可以用此账号登录,重置SA密码. 但是在以下情况下,怎么办呢? 1. SA密码丢失或者SA账号被禁用. 2. 你进行了一些安全操作,把BuiltinAdm ...
- 【POJ3415】 Common Substrings (SA+单调栈)
这道是求长度不小于 k 的公共子串的个数...很不幸,我又TLE了... 解法参考论文以及下面的链接 http://www.cnblogs.com/vongang/archive/2012/11/20 ...
- 【字符串】后缀数组SA
后缀数组 概念 实际上就是将一个字符串的所有后缀按照字典序排序 得到了两个数组 \(sa[i]\) 和 \(rk[i]\),其中 \(sa[i]\) 表示排名为 i 的后缀,\(rk[i]\) 表示后 ...
- [luogu] P3809 【模板】后缀排序 (SA)
板子,照着题解打的倍增版. #include <iostream> #include <cstdio> #include <cstring> using names ...
- 【MVC】ASP.NET MVC Forms验证机制
http://www.cnblogs.com/bomo/p/3309766.html 随笔 - 121 文章 - 0 评论 - 92 [MVC]ASP.NET MVC Forms验证机制 ASP. ...
随机推荐
- ES5 与 ES6六大不同
1.类Class 2.模块Module 导出变量 导出函数 导入 3.箭头函数 4.不再支持Mixins. 5.ES6不再支持自动绑定.
- [MYSQL笔记0]MYSQL的安装
mysql是一种关系型数据库管理系统.以mysql5.7版本为例,安装过程如下: 首先百度出mysql的官网,进入:(以下是自己安装失败的过程,直接下拉最后看大佬的安装过程吧,就是那个红红的网址) 找 ...
- 银行卡验证API
一.银联开放平台 https://open.unionpay.com/tjweb/api/detail?apiSvcId=21 应用场景 综合数据服务平台是银联为接入商户提供的综合数据认证服务接口,目 ...
- CSS3解决移动端手指点击或滑动屏幕时出现的浅蓝色背景框
当时做完移动端页面发现点击图片(背景图按钮),出现了浅蓝色背景,实属影响页面美观,解决这一问题代码如下 html,body{ -webkit-tap-highlight-color: rgba(0,0 ...
- 模块pandas
python之pandas简单介绍及使用(一) https://www.cnblogs.com/misswangxing/p/7903595.html
- python 函数 闭包 (节省内存空间 html 获取网页的源码)
#闭包:嵌套函数,内部函数调用外部函数的变量 # def outer(): # a = 1 # def inner(): # print(a) # inner() # outer() def oute ...
- openwrt(一):openwrt源码下载及编译环境搭建
声明:从网上各位大神的博客学习,整理后记录,非原创. 注:请用非root用户来下载源码 导航: 1. openwrt编译环境搭建 2. openwrt源码下载 3. feeds更新 1. openwr ...
- hihocoder #1394 : 网络流四·最小路径覆盖(最小路径覆盖)
#1394 : 网络流四·最小路径覆盖 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 国庆期间正是旅游和游玩的高峰期. 小Hi和小Ho的学习小组为了研究课题,决定趁此机 ...
- Android onConfigurationChanged 收不到回调
我返现,90度横屏 旋转到270度横屏onConfigurationChanged 是收不到回掉的.尽管清单里面声明了什么: android:configChanges="orientati ...
- 自定义控件的getChildCount
我真的是一步一步走过来,看过来的代码.不是能力问题,而是他们用的,我没用过,我用的他们不用.然后一句一句的问为什么,然后一句一句的去想为什么. 只有这样,才能慢慢的熟悉,东一榔头西一棒子,不是分模块再 ...