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. [Swust OJ 566]--开N方数(牛顿切线法解高次方程)

    题目链接:http://acm.swust.edu.cn/problem/0566/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  2. PHP学习笔记2-流程控制

    条件控制:if <?php function getLevel($score){ if($score>=90){ return "优秀"; }elseif($score ...

  3. Python框架 Flask 项目实战教程

    本文目的是为了完成一个项目用到的flask基本知识,例子会逐渐加深.最好对着源码,一步一步走.下载源码,运行pip install -r requirements.txt 建立环境python db_ ...

  4. javascript 入门之简单换肤效果

    大家好,我是小强老师,这里简单入门 做一个换肤效果 效果如图所示: 这个案例思路分为两部分: 获取元素对象. var pic1 = document.getElementById('pic1'); v ...

  5. guozhongCrawler的是一个无须配置、便于二次开发

    guozhongCrawler的是一个无须配置.便于二次开发的爬虫开源框架,它提供简单灵活的API,只需少量代码即可实现一个爬虫.模块化设计完全 面向业务提供接口,功能覆盖整个爬虫的生命周期(链接提取 ...

  6. k路归并(败者树,记录败者)

          败者树在外排序中用到,每加入一个数字时,调整树需要o(lgk),比较快.外排序过程主要分为两个阶段:(1)初始化各归并段写入硬盘,初识化的方法,可利用内排序方法还可以一种叫置换选择排序的方 ...

  7. Search Insert Position--寻找插入点Given a sorted array and a target value, return the index if the target

    问题:链接 Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  8. Lucene核心--构建Lucene搜索(上篇,理论篇)

    2.1构建Lucene搜索 2.1.1 Lucene内容模型 一个文档(document)就是Lucene建立索引和搜索的原子单元,它由一个或者多个字段(field)组成,字段才是Lucene的真实内 ...

  9. DP HDIJ1421 搬宿舍

    Problem Description 搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆, ...

  10. svn回滚版本2

    svn 版本回滚 取消对代码的修改分为两种情况:   第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn ...