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
题目意思是:找出m个串中最长连续公共串,串长度要大于等于3,并且当最长公共串有多个,则输出字典序最小.
解析:这里假设有3个串,长度设短一点为8,程序只有16MS
1:ABCDAFKD  2:BCDAKJSA  3: KACBCDAD 
以1串中的子串与2,3串匹配。分解1串成子串:
len=8:  ABCDAFKD
len=7:  ABCDAFK  
 BCDAFKD
len=6:  ABCDAF
 BCDAFK
  CDAFKD
len=5:  ABCDA
 BCDAF
  CDAFK
   DAFKD
len=4:  ABCD
 BCDA
  CDAF
   DAFK
    AFKD
len=3:  时己经不用判断了,最长公共串为BCDA。
就按上面的子串一一与2,3串进行匹配。
#include<stdio.h>
char str[12][65],ch[65];
void panduan(int l,int r,int flog)
{
int i,j;
if(flog==0)
{
for( i=0;l<=r;l++,i++) ch[i]=str[1][l];
return ;
}
for( i=0,j=l;j<=r;i++,j++)
if(str[1][j]<ch[i]) break;
if(j<=r)
{
for(i=0,j=l;j<=r;j++,i++) ch[i]=str[1][j];
}
}
int main()
{
int cas,m,l,r;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&m);
for(int k=1;k<=m;k++)
scanf("%s",str[k]);
int len,mov,k,t,i,j,flog=0;
for(len=60;len>=3;len--)//最长分共长度为len
{
l=0; r=len-1;//第1个串中最长公共串的最左,右
for(mov=0;mov<=60-len;mov++)//在长度为len下,在串1中有60-len种长度为len的串
{
l=mov; r+=mov;//长度为len的串在串1中的位置
for(k=2;k<=m;k++)//与第2~m个串匹配
{
for( t=0;t<=60-len;t++)//在第k个串中以t位置为带头进行匹配
{
for(i=l,j=t;i<=r;j++,i++)
if(str[1][i]!=str[k][j]) break;//在第k串中没找到完全匹配
if(i>r) break;//在第k串中找到匹配的串
}
if(t>60-len) break;//在第k个串中没有找到匹配的那么一定不是公共串,那后串就不用找了
}
if(k>m){//找到最长公共串
panduan(l,r,flog);//找到最长公共串中字典序最小的
flog=1;
}
r-=mov;//复原
}
if(flog) break;//找到最长公共串
}
if(flog)
{
for(i=0;i<len;i++) printf("%c",ch[i]);
printf("\n");
}
else
printf("no significant commonalities\n");
}
}
												

最新文章

  1. Circular Queue Implementation Principle
  2. C++调用shell
  3. 一步一步来做WebQQ机器人-(二)(第一次登陆)
  4. day6 - 面向对象学习
  5. groovy-正则表达式
  6. 神秘代码让iPhone微信闪退的解决方法
  7. TeeChart中 Line的Clear方法
  8. 去掉VC2010 编辑器里出现的红色波浪线
  9. 【原】浅谈Firefox下的js、css、图片阻塞现象(一)
  10. 《Effective C#》读书笔记-1.C# 语言习惯-2.使用运行时常量(readonly)而不是编译时常量(const)
  11. java constructor 在构造子类时,一定会调用到父类的构造方法 “ 私有属性被继承了?”问题
  12. SDN第四次作业
  13. 修改firefox的默认缩放比
  14. TCP连接建立系列 — 服务端发送SYNACK段
  15. java8list排序
  16. JavaScript判断该对象是否为数组
  17. Dockerfile指令详解
  18. 统计分页一些sql
  19. 【计算机网络】数据交换技术和多路复用技术的正(nao)确(can)打开方式
  20. 【python】python读取文件报错UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode byte 0xac in position 2: illegal multibyte sequence

热门文章

  1. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
  2. Basic DataList
  3. CentOS服务器下对mysql的优化
  4. CodeForces 385C Bear and Prime Numbers 素数打表
  5. USACO Milk2 区间合并
  6. smarty 截取字符串,调用php中的方法,foreach循环
  7. 临时节点不能有child 子节点
  8. sequence2(高精度dp)
  9. android的animator
  10. 高级UIKit-01(总结基础UIKit)