【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. ...
随机推荐
- c++ 11 线程池的简单封装
#include <condition_variable> #include <queue> #include <thread> #include <vect ...
- 漂亮提醒框js
<script type="text/javascript"> var filename = "PICC_V2.1.3.0_新增功能操作手册.doc" ...
- dataTables设置下拉滚动出现表头挤在一起的解决方法
1.引入datatable的CSS文件 <link href="http://cdn.datatables.net/1.10.15/css/jquery.dataTables.min. ...
- C#基础-异常处理与自定义异常
异常处理 static void Main(string[] args) { Console.WriteLine("请输入一个数字:"); try { // 监测可能出现异常代码 ...
- IDEA的下载安装和激活
1.下载网站http://www.jetbrains.com/ 2. 3. 4.注意要下载Ultimate版本 5.安装 下载完成后直接下一步,傻瓜式安装 6.激活,在图片位置输入激活码即可 !!!! ...
- jquery横向手风琴效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- zeppelin的数据集的优化
前面我们介绍了zeppelin的修改,前面由于自己的原因,对zeppelin的修改过于多,现在由于优化了,我们两个类, 一个是zeppelin-server的NotebookServer的类的broa ...
- Hive LanguageManual DDL
hive语法规则LanguageManual DDL SQL DML 和 DDL 数据操作语言 (DML) 和 数据定义语言 (DDL) 一.数据库 增删改都在文档里说得也很明白,不重复造车轮 二.表 ...
- IIS相关服务和无法启动服务W3SVC错误提示
首先,打开“服务”查看下面的服务是否启动.(下面的两个服务就是跟IIS相关的服务,计算机(右键)->管理->服务和应用程序->服务) Windows Process Activati ...
- 程序员必需知道的Windows Shell命令
Windows系统本来就很人性化的操作系统,操作很方便,但是对于开发人员来说,有些时候改变一些电脑配置或者执行某些任务来说,使用GUI操作反而事倍功半,因此建议使用Shell命令来提高一下工作效率. ...