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 这个代码说明了STL真心强大,不过效率有点低~~~(比较的次数有点多)

#include<iostream>
#include<string.h>
#define MAXN 60
using namespace std;

int main()
{
int t;
cout<<"请输入测试次数"<<endl;
cin>>t; //测试数据的次数
while(t--)
{
int n; //每组测试数据有多少行数据
cout<<"请输入此数据的行数"<<endl;
cin>>n;
string str[n]; //用以存放数据
for(int i=0;i<n;i++)
{
cin>>str[i];
}
string res = " "; //用于存储找到的符合要求的子串
for(int i=3;i<=MAXN;i++) //i表示子串的长度
{
for(int j=0;j<MAXN;j++) //j表示字符串中的下标位置
{
string tmp=str[0].substr(j,i);
bool flag=true;
for(int k=1;k<n;k++)
{
if(str[k].find(tmp)==string::npos)
{
flag=false;
break;
}
}
if(flag&&res.size()<tmp.size()) res=tmp;                      //获得较长的子串
else if(flag&&(res.size()==tmp.size())&&(tmp<res)) res=tmp;    //如果长度相等,则获得获得较小的字符串
}
}
if(res==" ") cout<<"no significant commonalities"<<endl;
else cout<<res<<endl;
}
return 0;
}

这道题跟算法中的求最长公共子序列有点像,不过区别是算法中的题的子串可以是不连续的,子要存在该字符串就可以了,采用的是递归回溯的方法,此处是连续的

求最长连续公共子序列 POJ 3080的更多相关文章

  1. 最长连续公共子序列(LCS)与最长递增公共子序列(LIS)

    最长公共子序列(不连续) 实际问题中也有比较多的应用,比如,论文查重这种,就是很实际的一个使用方面. 这个应该是最常见的一种了,不再赘述,直接按照转移方程来进行: 按最普通的方式就是,直接构造二维矩阵 ...

  2. poj3080Blue Jeans(在m个串中找到这m个串的 最长连续公共子序列)

    Description The Genographic Project is a research partnership between IBM and The National Geographi ...

  3. hdoj1423 最长上升公共子序列

    hdoj1423 题目分析: 两个数组a[n1] , b[n2], 求最长上升公共子序列. 我们可用一维存储 f[i] 表示 b 数组以 j 结尾, 与 a[] 数组构成的最长公共上升子序列. 对数组 ...

  4. HDU 3308 线段树求区间最长连续上升子序列长度

    题意:两种操作,Q L R查询L - R 的最长连续上升子序列长度,U pos val 单点修改值 #include <bits/stdc++.h> #define N 100005 us ...

  5. 最长公共子序列(LCS)、最长递增子序列(LIS)、最长递增公共子序列(LICS)

    最长公共子序列(LCS) [问题] 求两字符序列的最长公共字符子序列 问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...

  6. HDU 3308 线段树 最长连续上升子序列 单点更新 区间查询

    题意: T个测试数据 n个数 q个查询 n个数 ( 下标从0开始) Q u v 查询 [u, v ] 区间最长连续上升子序列 U u v 把u位置改成v #include<iostream> ...

  7. pta 习题集 5-5 最长连续递增子序列 (dp)

    给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列.例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8). 输入格式: 输入第1行给出正整数n ...

  8. JDOJ 1946 求最长不下降子序列个数

    Description 设有一个整数的序列:b1,b2,…,bn,对于下标i1<i2<…<im,若有bi1≤bi2≤…≤bim 则称存在一个长度为m的不下降序列. 现在有n个数,请你 ...

  9. P1020 导弹拦截(nlogn求最长不下降子序列)

    题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...

随机推荐

  1. 介绍两个Android不常用的Drawable:GradientDrawable和 StateListDrawable

    //-------------------------------------------------------------------------------------------------- ...

  2. 关于glibc中的res_init()函数

    /* * Set up default settings.  If the configuration file exist, the values * there will have precede ...

  3. loadrunner基本概念、安装及术语(一)

    一.初识loadrunner: LoadRunner,是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadRunner能够对整个企业架 ...

  4. Android.mk与jni目录的关系

    附1: Android.mk与jni目录的关系: 在某目录下,如/src/modules1/下存放Android.mk和Application.mk,调用ndk-build试图编译时,会遇到如下错误: ...

  5. IE6 下 输入类型表单控件背景问题

    .box input{background:url(img/1.jpg) fixed} <body> <div class="box"> <input ...

  6. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  7. FZU 1912 Divisibility by Thirty-six(整除问题+字符串维护+贪心)

    这个整除36的与整除45的完全一样,就是被4整除的有点多,但都是两位数,所以枚举后面两位就可以了. #include <stdio.h> #include <string.h> ...

  8. POJ - 3062 Borg Maze

    题目链接:http://poj.org/problem?id=3026 Svenskt Masterskap我程序员/ Norgesmesterskapet 2001 Description The ...

  9. Windows下python安装MySQLdb

    安装MySQLdb需要在电脑上安装MySQL connector C,只需要这个connector就好,不需要把mysql装全. 另外,需要安装VC for python提供编译. 到官网上下载脚本进 ...

  10. shortcut to open a linux terminal

    1) alt+ f2 2) input "gnome-terminal" 3) press "enter"