Poj3080 -- blue jeans (brute force + String Matching)

Source: Internet
Author: User

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 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
Aacatcatcatttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
Sample output
No significant commonalities
Agatac
Catcatcat

Question:

Input n DNA sequences, each of which has a length of 60.

Find the longest co-occurrence subsequence.

PS1: If the length is the same, the minimum output lexicographically

PS2: If the longest part of The PS2 sequence is less than 2, no significant commonalities is output.

Solution:

Each subsequence of the first DNA sequence is enumerated in brute force mode. The strstr () function is used to match the 2-N sequence.

Code:

 1 #include<stdio.h> 2 #include<string> 3 #include<cstring> 4 #include<iostream> 5 using namespace std; 6 char str[100][100]; 7 void cpy(char *tmp,int i,int j) 8 { 9     int t=0,k;10     for (k=i; k<=j; k++)11         tmp[t++]=str[1][k];12     tmp[t]=0;13 }14 void cmp(char *ans,char *tmp)15 {16     if (strlen(ans)<strlen(tmp)) strcpy(ans,tmp);17     else if (strlen(ans)==strlen(tmp)&&strcmp(ans,tmp)>0)18         strcpy(ans,tmp);19 }20 int main()21 {22     int T;23     cin>>T;24     while (T--)25     {26         int N;27         char tmp[100];28         cin>>N;29         char ans[1000]= {0};30         for (int i=1; i<=N; i++)31             cin>>str[i];32         for (int i=0; i<=59; i++)33             for (int j=i; j<=59; j++)34             {35                 cpy(tmp,i,j);36                 int k;37                 for (k=2; k<=N; k++)38                     if (strstr(str[k],tmp)==NULL) break;39                 if (k==N+1) 40                     cmp(ans,tmp);41             }42         if (strlen(ans)>=3) printf("%s\n",ans);43         else printf("no significant commonalities\n");44     }45     return 0;46 }

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.