Poj 3080 blue jeans (KMP)

Source: Internet
Author: User
Tags first string

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: The number of test data groups first, followed by the number of strings in each group of data. Each group of strings contains 60 characters, and the longest common substring is obtained, if the longest common substring is less than 3
No significant commonalities; otherwise, the longest public substring with the smallest Lexicographic Order is output.
Idea: enumerate the substrings of the first string as a comparison between the template and other strings.
AC code:
1 # include <stdio. h> 2 # include <string. h> 3 char S1 [200], S2 [15] [200]; 4 int next [200], CNT, N1, N2, N, Ma; 5 void nextshuzu () // next array 6 {7 int I = 0, j =-1; 8 next [0] =-1; 9 While (I <N1) 10 {11 if (j =-1 | S1 [I] = S1 [J]) 12 next [++ I] = ++ J; 13 else14 J = next [J]; 15} 16} 17 void KMP () 18 {19 nextshuzu (); 20 int I = 0, j = 0; 21 Ma = 100; 22 For (int K = 1; k <n; k ++) 23 {24 int m = 0; 25 while (j <N2 & I <60) 26 {27 if (j =-1 | S1 [J] = S2 [k] [I]) 28 {29 I ++; 30 J ++; 31} 32 else33 J = next [J]; 34 if (j> m) // obtain the maximum value of this substring 35 m = J; 36} 37 If (M <Ma) // retrieve the smallest one, which must be 38 Ma = m; 39} 40} 41 int main () 42 {43 int B; 44 char s [200]; 45 scanf ("% d", & B); 46 while (B --) 47 {48 scanf ("% d", & N); 49 for (INT I = 0; I <n; I ++) 50 scanf ("% s", S2 [I]); 51 int ans = 0; 52 for (INT I = 0; I <58; I ++) 53 {54 strcpy (S1, S2 [0] + I); 55 n2 = 60-i; 56 KMP (); 57 if (ANS <Ma) 58 {59 ans = MA; 60 strncpy (S, S2 [0] + I, ANS); 61 s [ANS] = '\ 0 '; 62} 63 else if (MA = ans) // I didn't write it here for the first time. After WR, I read other people's blogs. First of all, thank you, here I used 64 {// If the length is the same, use the 65 char S3 [200] with the smallest lexicographically; 66 strncpy (S3, S2 [0] + I, ans); 67 S3 [ANS] = '\ 0'; 68 if (strcmp (S, S3)> 0) 69 strcpy (S, S3 ); 70} 71} 72 If (ANS> 2) 73 printf ("% s \ n", S); 74 else75 printf ("no significant commonalities \ n "); 76} 77}

 

 

Poj 3080 blue jeans (KMP)

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.