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

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

Sample Output

no significant commonalities
AGATAC
CATCATCAT

Source

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
using namespace std;
typedef long long LL;
#define MAXN 63
/*
枚举所有子串,用KMP算法检查是否出现过,选择其中的最优解
*/
char s[MAXN][MAXN];
int next[MAXN];
void kmp_pre(char x[])
{
int i,j,m=strlen(x);
j = next[] = -;
i = ;
while(i<m)
{
if(j!=-&&x[i]!=x[j])
j = next[j];
next[++i] = ++j;
}
}
bool kmp(char x[],char y[])
{
int i,j,ans = ,m=strlen(x),n=strlen(y);
kmp_pre(x);
i=j=;
while(i<n)
{
while(j!=-&&y[i]!=x[j])
j = next[j];
i++;
j++;
if(j>=m)
return true;
}
return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int m;
char ans[MAXN] = "Z";
scanf("%d",&m);
for(int i=;i<m;i++)
scanf("%s",s[i]);
for(int len=;len>=;len--)
for(int i=;i<=-len;i++)
{
char b[MAXN] = {};
strncpy(b,s[]+i,len);
//cout<<len<<":"<<b<<endl;
int j;
for(j=;j<m;j++)
{
if(!kmp(b,s[j]))
break;
}
if(j==m&&strcmp(ans,b)>)
{
strcpy(ans,b);
}
if(ans[]!='Z'&&i==-len)
{
i = ;
len = ;
}
}
if(ans[]=='Z')
printf("no significant commonalities\n");
else
printf("%s\n",ans);
}
return ;
}

Blue Jeans POJ 3080 寻找多个串的最长相同子串的更多相关文章

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

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

  2. Blue Jeans - POJ 3080(多串的共同子串)

    题目大意:有M个串,每个串的长度都是60,查找这M个串的最长公共子串(连续的),长度不能小于3,如果同等长度的有多个输出字典序最小的那个.   分析:因为串不多,而且比较短,所致直接暴力枚举的第一个串 ...

  3. Match:Blue Jeans(POJ 3080)

    DNA序列 题目大意:给你m串字符串,要你找最长的相同的连续字串 这题暴力kmp即可,注意要按字典序排序,同时,是len<3才输出no significant commonalities #in ...

  4. Blue Jeans - poj 3080(后缀数组)

    大致题意: 给出n个长度为60的DNA基因(A腺嘌呤 G鸟嘌呤 T胸腺嘧啶 C胞嘧啶)序列,求出他们的最长公共子序列 使用后缀数组解决 #include<stdio.h> #include ...

  5. POJ 3294 出现在至少K个字符串中的子串

    在掌握POJ 2774(两个串求最长公共子串)以及对Height数组分组后,本题还是容易想出思路的. 首先用字符集外的不同字符连接所有串,这是为了防止两个后缀在比较时超过某个字符串的分界.二分子串的长 ...

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

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

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

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

  8. POJ 3080 Blue Jeans(Java暴力)

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

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

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

随机推荐

  1. poi 和jxl导出excel(2)

    controller: /** * 导出报表 * @return */ @RequestMapping(value = "/export") @ResponseBody publi ...

  2. 最少拦截系统------LCS--------动态规划

    这是一道极好的题,会了这个应该说 最长递增子序列什么的  就有了另外一种思路了 下面附上代码---应该仔细的看一下  那个  if判断 #include<stdio.h> #include ...

  3. (转)Vue 爬坑之路(三)—— 使用 vue-router 跳转页面

    使用 Vue.js 做项目的时候,一个页面是由多个组件构成的,所以在跳转页面的时候,并不适合用传统的 href,于是 vue-router 应运而生. 官方文档: https://router.vue ...

  4. Android:用签名打包后微信分享失效

    刚开始使用微信分享,申请的微信appid也可以在直接使用,分享成功! 当我使用自己的签名打包分享时却分享失败,一闪而过,好郁闷的说,为什么之前没有打包就可以,签名打包后就不可以了... 开始查找各种资 ...

  5. activity间传递参数

    传递值对象 值对象可以理解为自定义的数据类型对象. 为了完成这个知识点的讲解,先来创建一个User类型的类,它有name和age两个属性,然后请添加getter/setter方法,构造方法等基本方法. ...

  6. Java jre7及以上版本中的switch支持String的实现细节

    Java7中的switch支持String的实现细节 作者: zsxwing 更新: 2013-03-04 21:08:02 发布: 2012-04-26 13:58:19 在Java7之前,swit ...

  7. JS——对象创建

    1.原始创建 <script> person = new Object();//不要var person.firstname = "Bill"; person.last ...

  8. pengyue-form 模块 dropdown 关系联动

    <script> window.onload=function() { var school= document.getElementById("dnn_ctr5973_View ...

  9. MVC5+EasyUI+EF6+Linq通用权限系统出炉(1)

    1.先晒一下结构吧,

  10. ES6 作用域的问题