POJ 3080 Blue Jeans (longest Public String)

Source: Internet
Author: User

Blue Jeans
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submissions: 9973 Accepted: 4210

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

3
2
Gataccagataccagataccagataccagataccagataccagataccagataccagataccagata
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
3
Gataccagataccagataccagataccagataccagataccagataccagataccagataccagata
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
Catcatcatccccccccccccccccccccccccccccccccccccccccccccc
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Aacatcatcattttttttttttttttttttttttttttttttttttttttttttttttttttttttttttsample Output

No significant commonalities
AGATAC
CATCATCAT
Source

South Central USA 2006


Question: The longest Public String. If the maximum length is less than 3, no significant commonalities is output. Otherwise, the longest string is output.
Analysis: enumerate the length of a public string, and then search for a substring of the corresponding length in string 1 from 2nd to N.
Feeling: It is really not good to dynamically apply for memory space for new or 2D arrays. In addition, I didn't know the strstr () function at the beginning. I had been very hard-pressed to find the function of the corresponding substring, and I still couldn't run it. Changed a lot
Methods, such as first finding the substring of the first and second strings, then finding the public part of the third and all found substrings, and so on, finally, they were all written Yellow by themselves ....
Knowledge Point sorting:
1,


Include File: string. h
Function Name: strstr
Function prototype: extern char * strstr (char * str1, char * str2 );
Function: Check whether str2 exists in str1. If str2 exists, the pointer from the start position of str2 in str1 is returned. If no, null is returned.
Return Value: returns the pointer to this position. If no pointer is found, a null pointer is returned.
Example:
123 char

Str [] = "1234
Xyz "; char *
Str1 = strstr (str, "34"); cout <str1 <endl;

Display: 34 xyz
2,
Prototype: extern int strcmp (const char * s1, const char * s2 );
Header file: string. h
Function: Compares s1 and s2 strings.
General Format: strcmp (string 1, string 2)
Note:
When s1 <s2, the return value is-1.
When s1 = s2, the return value is 0.
When s1> s2, the return value is 1.
Note: c ++ Medium
When s1 <s2, the return value is less than 0.
When s1 = s2, the return value is equal to 0.
When s1> s2, the return value is greater than 0.
That is, two strings are compared from left to right one by one (compare by ASCII value) until different characters or '\ 0' appear. For example:
"A" <"B" a ">" A "" computer ">" compare"
Note: strcmp (const char * s1, const char * s2) Only compares strings, numbers, and other parameters.
Code:
 

?#include<cstdio> #include<iostream> #include<cstring> using namespace std; const int len=60;  int main() {     int t,n,i,j,k;     scanf("%d",&t);     while(t--)     {         scanf("%d",&n);         char **DNA=new char*[n];         for(i=0;i<n;i++)         {             DNA[i]=new char[len+1];             scanf("%s",DNA[i]);         }         char obj[len+1];         int length=1;         int Strlen=0;         for(j=0;;j++)         {             char dna[len+1];             int pj=j;             if(pj+length>len)             {                 length++;                 j=-1;                 if(length>len)                     break;                 continue;             }             for(k=0;k<length;k++)                 dna[k]=DNA[0][pj++];             dna[k]='\0';              bool flag=true;             for(i=1;i<n;i++)             {                 if(!strstr(DNA[i],dna))                 {                     flag=false;                     break;                 }             }             if(flag)             {                 if(length>Strlen)                 {                     Strlen=length;                     strcpy(obj,dna);                 }                 else if(length==Strlen)                 {                     if(strcmp(obj,dna)>0)                         strcpy(obj,dna);                 }             }         }         if(Strlen<3)             printf("no significant commonalities\n");         else             printf("%s\n",obj);         delete DNA;     }     return 0; } #include<cstdio>#include<iostream>#include<cstring>using namespace std;const int len=60;int main(){    int t,n,i,j,k;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        char **DNA=new char*[n];        for(i=0;i<n;i++)        {            DNA[i]=new char[len+1];            scanf("%s",DNA[i]);        }        char obj[len+1];        int length=1;        int Strlen=0;        for(j=0;;j++)        {            char dna[len+1];            int pj=j;            if(pj+length>len)            {                length++;                j=-1;                if(length>len)                    break;                continue;            }            for(k=0;k<length;k++)                dna[k]=DNA[0][pj++];            dna[k]='\0';            bool flag=true;            for(i=1;i<n;i++)            {                if(!strstr(DNA[i],dna))                {                    flag=false;                    break;                }            }            if(flag)            {                if(length>Strlen)                {                    Strlen=length;                    strcpy(obj,dna);                }                else if(length==Strlen)                {                    if(strcmp(obj,dna)>0)                        strcpy(obj,dna);                }            }        }        if(Strlen<3)            printf("no significant commonalities\n");        else            printf("%s\n",obj);        delete DNA;    }    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.