题目大意:

求出这些DNA序列中的最长且字典序最小的公共子串。

思路分析:

二分长度的答案,去height中扫描这个长度是否满足,一旦满足就立即输出。这样就能够保证字典序最小了。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define maxn 1005
using namespace std; char str[maxn];
int sa[maxn],t1[maxn],t2[maxn],c[maxn],n; void suffix(int m)
{
int *x=t1,*y=t2;
for(int i=0;i<m;i++)c[i]=0;
for(int i=0;i<n;i++)c[x[i]=str[i]]++;
for(int i=1;i<m;i++)c[i]+=c[i-1];
for(int i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
for(int k=1;k<=n;k<<=1)
{
int p=0;
for(int i=n-k;i<n;i++)y[p++]=i;
for(int i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
for(int i=0;i<m;i++)c[i]=0;
for(int i=0;i<n;i++)c[x[y[i]]]++;
for(int i=0;i<m;i++)c[i]+=c[i-1];
for(int i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1;x[sa[0]]=0;
for(int i=1;i<n;i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
if(p>=n)break;
m=p;
}
}
int rank[maxn],height[maxn];
void getheight()
{
int k=0;
for(int i=0;i<n;i++)rank[sa[i]]=i;
for(int i=0;i<n;i++)
{
if(k)k--;
if(!rank[i])continue;
int j=sa[rank[i]-1];
while(str[i+k]==str[j+k])k++;
height[rank[i]]=k;
}
}
int pos,N,fans;
bool vis[20];
int bel[maxn]; bool ok()
{
for(int i=1;i<=N;i++)
if(!vis[i])return false;
return true;
}
bool check(int len)
{
pos=-1;
memset(vis,false,sizeof vis); int i;
for(i=1;i<n;i++)
{
if( height[i]<len )
{
if(ok())
{
pos=sa[i-1];
return true;
}
memset(vis,false,sizeof vis);
}
else
{
if(!vis[bel[sa[i-1]]])vis[bel[sa[i-1]]]=true;
if(!vis[bel[sa[i]]])vis[bel[sa[i]]]=true;
if(ok())
{
pos=sa[i];
return true;
}
}
}
if(ok())pos=sa[i-1];
return pos!=-1;
}
char tmp[100];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
int top=0;
for(int i=1;i<=N;i++)
{
scanf("%s",tmp);
for(int j=0;j<60;j++)
{
bel[top]=i;
str[top++]=tmp[j];
}
bel[top]=i;
str[top++]=127-i;
}
str[top-1]=0; n=top; suffix(128);
getheight(); int l=3,r=60,mid,ans=0;
while(l<=r)
{
mid=(l+r)>>1;
if(check(mid))ans=mid,fans=pos,l=mid+1;
else r=mid-1;
}
if(ans<3)printf("no significant commonalities");
else
{
for(int i=fans;i<fans+ans;i++)printf("%c",str[i]);
}
puts("");
}
return 0;
}

POJ 3080 Blue Jeans (后缀数组)的更多相关文章

  1. POJ 3080 Blue Jeans 后缀数组, 高度数组 难度:1

    题目 http://poj.org/problem?id=3080 题意 有m个(2<=m<=10)不包含空格的字符串,长度为60个字符,求所有字符串中都出现过的最长公共子序列,若该子序列 ...

  2. POJ 3080 Blue Jeans (求最长公共字符串)

    POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...

  3. POJ 3080 Blue Jeans(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通 ...

  4. POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用

    题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...

  5. poj 3080 Blue Jeans

    点击打开链接 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10243   Accepted: 434 ...

  6. POJ 3080 Blue Jeans (字符串处理暴力枚举)

    Blue Jeans  Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 21078        Accepted: ...

  7. POJ 3080 Blue Jeans(Java暴力)

    Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...

  8. POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20966   Accepted: 9279 Descr ...

  9. poj 3080 Blue Jeans 解题报告

    题目链接:http://poj.org/problem?id=3080 该题属于字符串处理中的串模式匹配问题.题目要求我们:给出一个DNA碱基序列,输出最长的相同的碱基子序列.(保证在所有的序列中都有 ...

随机推荐

  1. git clone 出现错误

    看了好多资料终于搞定了git 中clone命令报错这个问题,废话不多说直接上步骤希望对大家有帮助. 1   删除.ssh文件夹(直接搜索该文件夹)下的known_hosts(手动删除即可,不需要git ...

  2. BZOJ 3831 单调队列DP

    思路: 这好像是我刚学单调性的时候做的题 (我是不会告诉你 我被这题教做人了的...) i-stk[head]>k 删队头 f[stk[tail]]>f[i]||(f[stk[tail]] ...

  3. do…while语句

    有些情况下,不论条件是否满足,循环过程必须至少执行一次,这时可以采用do...while语句.就像如图7.4所示登录账号一样,需要先输入密码和账户名,后进行判断:如果密码始终不正确,则循环要求用户输入 ...

  4. 复习java第五天(枚举、Annotation(注释) 概述)

    一.枚举 传统的方式: •在某些情况下,一个类的对象是有限而且固定的.例如季节类,只能有 4 个对象 •手动实现枚举类: —private 修饰构造器. —属性使用 private final 修饰. ...

  5. class path resource [processes/] cannot be resolved to URL because it does not exist

    springboot整合activiti时报以下错误,原因是项目启动时检查流程文件 nested exception is java.io.FileNotFoundException: class p ...

  6. wx小程序开发 1:小程序代码构成

    官网学习地址:https://developers.weixin.qq.com/miniprogram/dev/quickstart/basic/introduction.html 1: 2:待续.. ...

  7. 安卓 九宫格 GridView 的表格布局

    首先,请大家理解一下“迭代显示”这个概念,这个好比布局嵌套,我们在一个大布局里面重复的放入一些布局相同的小布局,那些重复的部分是由图片和文字组成的小控件,图片在上方,文字在下方,之后我们只需要把这些小 ...

  8. tomcat 热加载设置

    找到tomcat项目的apache-tomcat-8.0.30\conf\context.xml,打开进行编辑,把Context项中加上 reloadable="true" < ...

  9. 想要远程服务器长时间挂机不断开ssh连接的技巧

    使用top命令挂着就好了,top命令执行的“查看系统进程和资源占用”的任务会一直输出动态的数据,一直有数据传输就不会因为长时间挂机而断开ssh链接了,尤其针对于海外服务器,因为高延迟经常出现挂机久了自 ...

  10. eas之编辑表单元格

    --指定表列行单元不可编辑 // 锁定表格.行.列.单元 table.getStyleAttributes().getProtection().setLocked(true); row.getStyl ...