题目

Description

- The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you have been tasked with writing a program that will find commonalities amongst given snippets of DNA that can be correlated with individual survey information to identify new genetic markers. A DNA base sequence is noted by listing the nitrogen bases in the order in which they are found in the molecule. There are four bases: adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA sequence could be represented as TAGACC. Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.

Input

- Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components: A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset. m lines each containing a single base sequence consisting of 60 bases.

Output

- For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string "no significant commonalities" instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.

Sample Input 1

- 3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output 1

- no significant commonalities
AGATAC
CATCATCAT

思路

  • 暴力循环子串长度
  • \(KMP\)判断每个串中是否有该子串
  • 详细见\(code\)
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std; const int Max=1001;
int nx[Max];
string S[Max],P;
int T,M,N[Max]; void makenx(int M)
{
memset(nx,0,sizeof(nx));
int i=0,j=-1;
nx[i]=j;
while(i<M)
{
if(j==-1||P[i]==P[j]) i++,j++,nx[i]=j;
else j=nx[j];
}
} int Kmp(int k,int M)
{
int i=0,j=0;
while((i<N[k])&&(j<M))
{
if(j==-1||S[k][i]==P[j]) i++,j++;
else j=nx[j];
}
if(j>=M) return true;
else return false;
} int main()
{
int n,m;bool fl;
scanf("%d",&T);
string ans;int lans;
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++) cin>>S[i],N[i]=S[i].size();
ans="";lans=0;
m=S[1].size();
for(int i=0; i<m; i++)//循环子串起点
{
for(int j=3; j<=m-i; j++)//循环子串长度
{
fl=true;
P=S[1].substr(i,j);
M=j;makenx(M);
for(int k=2; k<=n; k++)
if(!Kmp(k,M))//Kmp判断
{fl=false;break;}
if(fl)
{
if(M>lans) ans=P,lans=M;
else if(M==lans&&ans>P) ans=P,lans=M;//字典序
}
}
}
if(ans=="") cout<<"no significant commonalities"<<endl;
else cout<<ans<<endl;
}
return 0;
}

Blue Jeans[poj3080]题解的更多相关文章

  1. POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()

    题目链接:https://vjudge.net/problem/POJ-3080 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total ...

  2. POJ3080——Blue Jeans(暴力+字符串匹配)

    Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...

  3. poj3080 Blue Jeans【KMP】【暴力】

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:21746   Accepted: 9653 Descri ...

  4. POJ 3080 Blue Jeans(Java暴力)

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

  5. POJ Blue Jeans [枚举+KMP]

    传送门 F - Blue Jeans Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  6. poj 3080 Blue Jeans

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

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

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

  8. (字符串 KMP)Blue Jeans -- POJ -- 3080:

    链接: http://poj.org/problem?id=3080 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...

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

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

随机推荐

  1. CodeForces 429B Working out DP

    E - Working out Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  2. 【转载】Linux进程间通信(六):共享内存 shmget()、shmat()、shmdt()、shmctl()

    来源:https://www.cnblogs.com/52php/p/5861372.html 下面将讲解进程间通信的另一种方式,使用共享内存. 一.什么是共享内存 顾名思义,共享内存就是允许两个不相 ...

  3. Centos7.5中Nginx报错:nginx: [error] invalid PID number "" in "/run/nginx.pid" 解决方法

    服务器重启之后,执行 nginx -t 是OK的,然而在执行 nginx -s reload 的时候报错 nginx: [error] invalid PID number "" ...

  4. codewars--js--Two Joggers--求最小公倍数、最大公约数

    问题描述: Two Joggers Description Bob and Charles are meeting for their weekly jogging tour. They both s ...

  5. 【题解】 2月19日 厦门双十中学NOIP2014模拟D2 T2 采药人接水果

    [问题描述] 采药人虽然 AFO(SU),但他在闲暇的时候还是可以玩一玩接水果(cat)的.但他渐渐发现 cat 好像有点太弱智.于是他不想浪费他的智商,于是决定写一个程序帮他玩. cat 是这样玩的 ...

  6. Linux下使用Nginx

    模拟tomcat集群 1.下载tomcat7,/usr/local下新建目录tomcat,将tomcat7剪切到/usr/local/tomcat wget http://mirror.bit.edu ...

  7. BIO&NIO

    在BIO中只有一个核心对象--Stream,它是单向的数据传输通道,即每个Stream要么是输入要么是输出的,不可兼得.开发人员是面向Stream进行编程的. 在NIO中有三个核心对象--Seleto ...

  8. 配置 Apache James 邮件服务器以使用加密邮件通讯协议

    可先参照: 使用 Apache James 3.3.0(开源免费) 搭建内网电子邮件服务器(基于 Windows + Amazon Corretto 8)https://www.cnblogs.com ...

  9. djinn:1 Vulnhub Walkthrough

    靶机下载链接: https://download.vulnhub.com/djinn/djinn.ova 主机端口扫描: FTP发现一些文件提示 1337端口是一个游戏,去看下 哈哈有点难,暂时放弃, ...

  10. vue2.0嵌套组件之间的通信($refs,props,$emit)

    vue的一大特色就是组件化,所以组件之间的数据交互是非常重要,而我们经常使用组件之间的通信的方法有:props,$refs和emit. 初识组件之间的通信的属性和方法 props的使用 子组件使用父组 ...