poj 3080 Blue Jeans

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

暴力思想:

开二维DNA[][]保存所有DNA序列

  以DNA[0]为母版,顺次截取60个长度length=1的子串dna[],检查其他DNA[i]是否都有子串dna,若是则把dna[]复制到obj[],否则枚举下一个长度length的子串;若obj与新的dna等长,则比较两者字典序,当dna字典序小时,复制到obj

 第1步循环60次后length+1。

顺次截取59个长度length=2的子串dna[],重复1的操作更新obj。。

 第2步循环59次后length+1。

顺次截取58个长度length=3的子串dna[],继续。。

.第59步循环2次后length+1。

顺次截取1个长度length=60的子串dna[],继续重复操作更新obj。

#include <iostream>
#include <string>
#include <cstring>

using namespace std;
int main()
{
    int len = 60;
    int t;
    cin>>t;
    int n;
    for(int tt = 0; tt < t; tt++)
    {
        cin>>n;
        char **DNA = new char*[n];
        for(int i = 0 ; i < n ; i++)
        {
            DNA[i] =  new char[61];
            cin>>DNA[i];
        }
        char obj[61]; //所有DNA的公共串
        int strLen = 0;//最长公共串长度
        int length = 1;//当前枚举的公共串长度 
        for(int i = 0 ; ; i++)
        {
            char dna[61];//枚举公共串dna[] 
            int pi = i;
/*截取DNA[0][]中以pi为起点,长度为length的子串dna[]*/  if(pi + length > len) { length++; i = -1; if(length > len) break; continue; } for(int j = 0 ; j < length; j++) { dna[j] = DNA[0][pi++]; } dna[length] = '';  /*检查其他DNA[][]是否都存在字符串dna[]*/  bool flag = true; for(int k = 1 ; k < n; k++) { if(!strstr(DNA[k],dna)) { flag = false; break; } } if(flag) { if(strLen < length) { strLen = length; strcpy(obj,dna); } else if(strLen == length) { if(strcmp(obj,dna) > 0) { strcpy(obj,dna);//存在相同长度的公共串时,取最小字典序的串 } } } } if(strLen < 3) cout<<"no significant commonalities"<<endl; else
cout<<obj<<endl; delete DNA; } return 0; }
原文地址:https://www.cnblogs.com/T8023Y/p/3259069.html