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

poj3080Blue Jeans(在m个串中找到这m个串的 最长连续公共子序列)的更多相关文章

  1. (字符串的处理4.7.16)POJ 1159 Palindrome(让一个字符串变成回文串需要插入多少个字符...先逆序,在减去公共子序列的最大长度即可)

    /* * POJ_1159.cpp * * Created on: 2013年10月29日 * Author: Administrator */ #include <iostream> # ...

  2. HDU 2087 剪花布条(模式串在主串中出现的次数主串中子串不可重叠)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题意:求模式串在主串中出现的次数,与模式串匹配的子串之间不可重叠. 思路:用kmp算法解决,在匹 ...

  3. HDOJ-2896(AC自动机+文本串中出现了哪几个模板串)

    病毒侵袭 HDOJ-2896 主要使用AC自动机解决,其次在query函数中改变一下,用来记录每个模板串出现的次数,还有insert函数中记录模板串的编号 需要注意最好使用结构体,而且不能一次性使用m ...

  4. 最长公共字串(LCS)最长连续公共字串(LCCS)

    链接1:http://blog.csdn.net/x_xiaoge/article/details/7376220 链接2:http://blog.csdn.net/x_xiaoge/article/ ...

  5. KMP小扩展,找出子串在主串中出现的所有位置

    KMP算法能够高效地匹配字符串,找出子串(T串)在主串(S串)中出现的首个位置的原算法网上已经有很多优秀的博文进行详细讲解,这里就不多赘述. 这篇博文主要是对KMP原算法稍作改动,使其能够在主串中把所 ...

  6. java 11-8 在大串中查找小串的案例

    1.统计大串中小串出现的次数 举例: 在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun& ...

  7. 【Leetcode】寻找数串中连续最大整数和且最大长度的子串

    寻找数串中连续最大整数和且最大长度的子串 输入示例: 1000 -100 200 -200 100 -100 10 90 输出结果: 1100 分析: 分治法解决问题非常方便,依然分为三种情况:a[1 ...

  8. java基础知识回顾之---java String final类普通方法的应用之“子串在整串中出现的次数”

    /* * 2 一个子串在整串中出现的次数. * "loveerlovetyloveuiloveoplove" * 思路: * 1,要找的子串是否存在,如果存在获取其出现的位置.这个 ...

  9. 浅谈数据结构之KMP(串中的模式匹配算法)

    KMP算法是一种模式匹配算法的改进版,其通过减少匹配的次数以及使主串不回朔来减少字符串匹配的次数,从而较少算法的相应代价,但是,事件万物是普遍归中的,KMP算法的有效性也是有一定的局限的,我将在本文的 ...

随机推荐

  1. OpenBlas编译方法(体验msys下使用MingW)

    OpenBlas是一个优化的Blas库,基于GotoBlas21.13 BSD版,安装步骤如下: Windows下安装: 1. 在SourgeForge下载最新的OpenBlas库:http://so ...

  2. PHP通过Thrift操作Hbase

    PHP通过Thrift操作Hbase     HBase是一个开源的NoSQL产品,它是实现了Google BigTable论文的一个开源产品,和Hadoop和HDFS一起,可用来存储和处理海量col ...

  3. Nutch 是一个开源Java 实现的搜索引擎

    Nutch 是一个开源Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具.包括全文搜索和Web爬虫. Nutch的创始人是Doug Cutting,他同时也是Lucene.Hado ...

  4. 基于visual Studio2013解决C语言竞赛题之0513字符拷贝

     题目 解决代码及点评 /************************************************************************/ /* 13. 将字符数 ...

  5. Ganglia 权威指南-安装Ganglia过程

    转自于:http://blog.csdn.net/xxd851116/article/details/21527055 http://www.dataguru.cn/article-3816-1.ht ...

  6. php 写内容入csv文件乱码解决方法

    加入BOM头即可 fwrite($fp,"\xEF\xBB\xBF"); 参考:http://justcoding.iteye.com/blog/1668308

  7. 仿StackOverflow开发在线问答系统

    仿StackOverflow开发在线问答系统 [第二期11月9日开课]使用Python Flask Web开发框架实现一套类似StackOverflow的在线问答平台LouQA,具备提问,回答,评论点 ...

  8. 基于visual Studio2013解决C语言竞赛题之0907删除记录

       题目

  9. Linux笔记——linux下的语音合成系统

    1.festival 安装:sudo apt-get install festival 使用: (SayText "Hello!") 2. espeek(ubuntu 自带) # ...

  10. Linux内核-系统调用

    Linux内核-系统调用 1.与内核通信 #系统调用在用户空间进程和硬件设备之间添加了一个中间层 作用:1.为用户空间提供了一种硬件的抽象接口 2.系统调用保证了系统的稳定和安全 3.出于每一个进程都 ...