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 ;
}

最新文章

  1. .NET面试题系列[13] - LINQ to Object
  2. 【转载】James Whittaker:经营成功的测试职业生涯
  3. cvLoadImage函数解析 cvLoadImageM()函数
  4. 【STL】- vector的用法
  5. 找第k大数,最坏时间复杂度O(n)
  6. 自定义栈类型,具有找到站内最小元素的min函数 ,且min(),pop(),push()函数的时间复杂度为O(1)
  7. hbase权威指南学习笔记--过滤器
  8. deeplearning.ai 人工智能行业大师访谈 Ian Goodfellow 听课笔记
  9. Zabbix Agent active主动模式监控
  10. web 参考网址
  11. Android初级教程理论知识(第五章页面跳转和数据传递)
  12. Linux文件与目录管理 - ls, cp, mv
  13. &lt;数据结构基础学习&gt;(三)Part 1 栈
  14. 1. 七种join的sql编写
  15. Centos7.4修改主机名HostName颜色及格式
  16. es集群数据库~基本安装
  17. dotnet core 发布环境变量配置 dev/stage/prod
  18. centos中软件源码简单的编译安装./configure,make ,make install
  19. linux系统查看IP地址,不显示IP地址或者只显示127.0.0.1
  20. FairyGUI编辑器制作Unity3D UI值得借鉴

热门文章

  1. 如何使js函数异步执行
  2. P4049 [JSOI2007]合金
  3. NS2学习笔记(二)
  4. HDU 5279 分治NTT 图的计数
  5. 1、Web MVC简介
  6. Offer收割_4
  7. VS2015环境配置: VS2015 未能正确加载“ResourceManagerPackage”包的问题
  8. 【转】 Java 集合系列07之 Stack详细介绍(源码解析)和使用示例
  9. 五分钟学习React(五):React两种构建应用方式选择
  10. Android开发高手课 - 02 崩溃优化(下):应用崩溃了,你应该如何去分析?