Poj 3080 blue jeans (brute force enumeration + KMP similar to poj 3450)

Source: Internet
Author: User
Blue Jeans
Time limit:1000 ms   Memory limit:65536 K
Total submissions:9909   Accepted:4180

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
Find the longest continuous common substring. The length of the substring is less than 3 and the output no sign... otherwise, the output Lexicographic Order with the same length is the smallest.
Solution: enumerate the first string with length from large to small to find the next array, and then match all the strings with the first one.
Address: Blue
Jeans
AC code:
# Include <iostream> # include <cstring> # include <string> # include <cstdio> using namespace STD; int next [62], N; char A [12] [62]; int lena0; // length of the mode string // my idea is to use the first string as the mode string, then, calculate the next matching void getnext (int sta, int Len) {int I, j; Len = sta + Len; // The relative position is changed to the absolute position: Next [sta] = STA, next [sta + 1] = sta; for (I = sta + 1; I <Len; I ++) {J = next [I]; while (J! = Sta & A [0] [I]! = A [0] [J]) J = next [J]; if (a [0] [I] = A [0] [J]) next [I + 1] = J + 1; else next [I + 1] = sta ;}} int KMP (int sta, int Len) {int I, J, K; len = sta + Len; For (k = 1; k <n; k ++) // The substring following the substring matches with the substring of the first substring {int flag = 0; j = sta; for (I = 0; I <60; I ++) {While (J! = Sta & A [k] [I]! = A [0] [J]) J = next [J]; if (a [k] [I] = A [0] [J]) J ++; if (j = Len) {flag = 1; break ;}} if (flag = 0) // if there is no match, 0 return 0;} is returned ;} return 1 ;}int main () {int I, J, K, T; scanf ("% d", & T); While (t --) {scanf ("% d", & N); If (n = 0) break; for (I = 0; I <n; I ++) scanf ("% s", a [I]); lena0 = 60; char res [62] = ""; for (I = lena0; I> = 1; I --) // from the length lena0 start to enumerate down {int flag1 = 0; // whether the length of I exists for (j = 0; j <= lena0-i; j ++) // start position of mode string decomposition {getnext (J, I); If (KMP (J, I) {If (strlen (RES) = 0 | strncmp (Res, a [0] + J, I)> 0) {// If the matching string is null or the lexicographically ordered string is left behind, the strncpy (Res, A [0] + J, I); Res [I] = '\ 0';} flag1 = 1 ;}} if (flag1) // if (I) {if (I> = 3) printf ("% s \ n", Res); break ;}} if (I <3) // The length of the match is less than 3 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.