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. Java面试题集(1-50)

    下面的内容是对网上原有的面试题集及答案进行了全面修订之后的内容(原来的题目有很多重复无用的题以及错误的答案),参照了JDK最新版本,删除了重复题目以及EJB2.x等无用内容,补充最新面试题.知识点巩固 ...

  2. Android 应用内HttpClient 与 WebView 共享 Cookie

    DefaultHttpClient httpclient=....; String toUrl="https://cap.cityu.edu.hk/studentlan/details.as ...

  3. 性能测试工具:AB

    ###################################################################################格式:ab -n 请求次数 -c ...

  4. memcache 在php存取中的应用

    当用户刷新网页或有大量用户访问网站时,就会产生大量数据库查询进程,这不但拖慢了网页打开速度,同时也给服务器带来了很大压力. 作为php菜鸟,今天刚刚接触到了 memcache 这个东东,于是自己跟着文 ...

  5. struct 和 class 不同点

    在 C++ 里面 struct 和 class 没有本质的差别 仅仅是成员和继承方式的默认不同 struct 是 public class 是 private 我的个人建议是仅仅要须要实现成员函数的就 ...

  6. [置顶] CSS+DIV总结

         HTML在Web飞速发展的过程中起着重要作用,有着重要地位.HTML初衷是为了表达标签(<p>.<table>)的内容信息.同时文档布局由浏览器来完成,不使用任何格式 ...

  7. SQL SERVER 2008R2sp1配置Database Mail –用SQL 数据库发邮件

    步骤1)创建配置文件和帐户 看图片吧,挺简单的: 中间略过的一些步骤,就点下一步即可. 下面我们测试一下: Step 2)配置邮件: 在完成账户和配置文件创建之后,我们需要配置Database Mai ...

  8. BZOJ 1260: [CQOI2007]涂色paint( 区间dp )

    区间dp.. dp( l , r ) 表示让 [ l , r ] 这个区间都变成目标颜色的最少涂色次数. 考虑转移 : l == r 则 dp( l , r ) = 1 ( 显然 ) s[ l ] = ...

  9. JAVA刚碰见的问题( java.lang.SecurityException: The jurisdiction policy files are not signed by a trusted signer)

    原文:刚碰见的问题 1.  failed to load the jni shared library jre bin server jvm.dll 解决:这个主要是eclipse的版本和安装的jdk ...

  10. 一、Mongo的安装

    注:学习为主,平台为WIN7 32位系统 一.Mongo的安装 1.1 下载 到官方下载地址(http://www.mongodb.org/downloads)去下载所需要的版本 1.2 安装与运行 ...