POJ 3080 Blue Jeans (后缀数组)
题目大意:
求出这些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 (后缀数组)的更多相关文章
- POJ 3080 Blue Jeans 后缀数组, 高度数组 难度:1
题目 http://poj.org/problem?id=3080 题意 有m个(2<=m<=10)不包含空格的字符串,长度为60个字符,求所有字符串中都出现过的最长公共子序列,若该子序列 ...
- POJ 3080 Blue Jeans (求最长公共字符串)
POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...
- POJ 3080 Blue Jeans(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通 ...
- POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用
题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...
- poj 3080 Blue Jeans
点击打开链接 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10243 Accepted: 434 ...
- POJ 3080 Blue Jeans (字符串处理暴力枚举)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21078 Accepted: ...
- POJ 3080 Blue Jeans(Java暴力)
Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
- poj 3080 Blue Jeans 解题报告
题目链接:http://poj.org/problem?id=3080 该题属于字符串处理中的串模式匹配问题.题目要求我们:给出一个DNA碱基序列,输出最长的相同的碱基子序列.(保证在所有的序列中都有 ...
随机推荐
- Gym - 101981G The 2018 ICPC Asia Nanjing Regional Contest G.Pyramid 找规律
题面 题意:数一个n阶三角形中,有多少个全等三角形,n<=1e9 题解:拿到题想找规律,手画开始一直数漏....,最后还是打了个表 (打表就是随便定个点为(0,0),然后(2,0),(4,0), ...
- C# 标准命名规范
笔者从事开发多年,有这样一种感觉,查看一些开源项目,如Spring.Apache Common等源码是一件赏心悦目的事情,究其原因,无外两点:1)代码质量非常高:2)命名特别规范(这可能跟老外的英语水 ...
- 【Python小试牛刀】循环
1.斐波那契数列 斐波那契数列,数列前两项为1,之后每一项都是前两项之和. #!/usr/bin/env python3 a, b = 0, 1 while b < 100: print(b) ...
- IntelliJ IDEA/PyCharm/WebStorm 2019.1.2 注册码激活
[IDEA2019.1.2最新版版本激活,直接查看底部] 网上IntelliJ IDEA激活方式大多均已失效,目前常用激活方式为License Server 激活: http://idea.imsxm ...
- Win10切换JDK版本
开发项目由于使用JDK版本不同,来回配置环境变量有点繁琐,用了一天百度得到的方法 1:安装不同版本的JDK,这个应该都可以完成 2:配置环境变量 CLASSPATH.;%JAVA_HOME%\lib\ ...
- Python启动浏览器Firefox\Chrome\IE
# -*- coding:utf-8 -*- import os import selenium from selenium import webdriver from selenium.webdri ...
- facade 模式和gateway模式的区别
原文:http://stackoverflow.com/questions/4422211/what-is-the-difference-between-facade-and-gateway-desi ...
- 【Oracle】设置快速恢复区及reset快速恢复区
快速恢复区 概念 是一个默认放置所有备份恢复操作有关文件的地方,包括:控制文件在线镜像.在线重做日志.归档日志.外来归档日志.控制文件镜像复制.数据文件镜像复制.RMAN备份片和闪回日志. 如果启用的 ...
- 【原创】python中文编码问题深入分析(三):python2.7文件读写中文编码问题
上一篇文章介绍和分析了python2.7中使用print遇到的中文编码问题的原因和解决方案,本篇主要介绍一下python2.7中执行文件读写可能遇到的编码问题. 1.文件读取 假如我们读取一个文件,文 ...
- Altova MapForce AMS/ACI/ISF自定义模板
目前为止,我在百度上得到关于MapForce的信息少之又少,所以把自己的一些经验写下来,与大家分享. 如果要生成xml的话,就可以直接创建xml架构当作数据的目标文件. 以下是我做的AMS&A ...