Poj-3080 Blue Jeans

Source: Internet
Author: User

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 cocould 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

32GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA3GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAGATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAAGATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA3CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample output

no significant commonalitiesAGATACCATCATCAT

Question:

A set of DNA base sequences is given to determine the longest base sequence that appears in all sequences!


Let's talk about the output problem: the output is the longest and the same base subsequence. If the length of the longest and the same base subsequence is less than 3, the output is the English string; if there are multiple sub-sequences with the same longest length, only the first sub-sequences sorted by lexicographically are output!


I am using the BF algorithm for this question. It's very violent!

However, since the string length is only 60, you don't have to worry about timeout!

I used the # include <cstring> header file functions --> strncpy (), strstr (), strcpy, in this way, you can deepen your understanding of these three functions! What do you do? I will comment on the following code!


It is much more convenient to write in this way, and the code looks clear, not too tedious and boring!

The Code is as follows: (the key steps are described in detail !)


# Include <stdio. h> # include <string. h ># include <iostream> using namespace STD; int main () {int T, M; char ss [13] [520]; CIN> T; while (t --) {CIN> m; For (INT I = 0; I <m; I ++) CIN> SS [I]; char STR [520], TE [520]; // TE [] is a temporary string! Int Len = strlen (ss [0]); int maxlong = 0; // set the current longest public string to 0! For (INT I = 0; I <Len; I ++) {// because the maximum string length must be greater than 3, j = I + 2; for (Int J = I + 2; j <Len; j ++) {int telen = J-I + 1; // temporary string length! Strncpy (Te, ss [0] + I, telen); // This function is used to copy a string of the specified length! Te [telen] = '\ 0'; int flag = 0; // mark whether the Te string is a public string from P [1] to M-1! For (int K = 1 ;! Flag & K <m; k ++) {// if not found if (strstr (ss [K], Te) = NULL) Flag = 1; // The strstr function is used to search for the first occurrence of a string in another string! If no, null or false is returned!} // The parameters before and after this function are strstr (another string's first address and one string's first address )! If (! Flag) // search! If (telen> maxlong | (telen = maxlong & strcmp (Te, STR) <0) // sort by lexicographically! {Maxlong = telen; strcpy (STR, Te) ;}}if (maxlong >= 3) cout <STR <Endl; else cout <"no significant commonalities" <Endl;} return 0 ;}

The following code is provided to solve the problem with the KMP algorithm!

#include<cstring>#include<stdio.h>#include<iostream>using namespace std;const int maxn = 70;char b[10][maxn], p[maxn];int n, ma, lenp, next[maxn];void kk(){    int i = 0, j = -1;    next[0] = -1;    while(i < lenp)    {        if(j == -1 || p[i] == p[j])        {            i ++; j ++;            next[i] = j;        }        else j = next[j];    }    int k, m;    ma = 100;    for(int k = 1; k < n; k ++)    {        i = 0; j = 0; m = 0;        while(i < 60 && j < lenp)        {            if(j == -1 || b[k][i] == p[j])            {                i ++; j ++;            }            else j = next[j];            if(j > m) m = j;        }        if(m < ma) ma = m;    }}int main(){    int t;    char a[maxn];    scanf("%d", &t);    while(t --)    {        scanf("%d", &n);        for(int i = 0; i < n; i ++)        scanf("%s", b[i]);        int ans = 0;        for(int i = 0; i <= 57; i ++)        {            strcpy(p, b[0] + i);            lenp = 60 - i;            kk();            if(ans < ma)            {                ans = ma;                strncpy(a, b[0] + i, ans);                a[ans] = '\0';            }            else if(ans == ma)            {                char tmp[maxn];                strncpy(tmp, b[0] + i, ans);                tmp[ans] = '\0';                if(strcmp(tmp, a) < 0)                strcpy(a, tmp);            }        }        if(ans >= 3)        printf("%s\n", a);        else        printf("no significant commonalities\n");    }    return 0;}


 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.