Poj 3080 blue jeans (KMP longest public substring)

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

 
Bytes

Sample output

 
No significant commonalitiesagataccatcatcat

Give you n DNA strings and find the public substrings with the largest length. If there are multiple output lexicographic orders, the minimum length is less than 3.

The length of each DNA string is 60, which can be decreased from the length of the substring to 60 in turn and enumerate all the substrings of this length. When a substring of a certain length is also another n-1 substring, this is us. the answer is required.

Determine if it is a substring of another DNA string and KMP it directly.

# Include <cstdio> # include <cstring> using namespace STD; const int n = 15, M = 65, L = 60; bool KMP (char s [], char P []) {int next [m]; next [0] =-1, next [1] = 0; int Plen = strlen (P), I = 0, j, slen = strlen (s); While (++ I <Plen-1) {J = next [I]; while (J! =-1 & P [I]! = P [J]) J = next [J]; next [I + 1] = J + 1;} I = J = 0; while (I <slen & J <Plen) {If (j =-1 | s [I] = P [J]) ++ I, ++ J; else J = next [J];} return J = Plen;} int main () {int cas, N, flag; char s [N] [m], tans [m], ANS [m]; scanf ("% d", & CAS); While (CAS --) {scanf ("% d", & N ); for (INT I = Flag = 0; I <n; ++ I) scanf ("% s", s [I]); For (INT LANs = L; LANs> 2 & flag = 0; -- LANs) {strcpy (ANS, "ZZ "); For (INT I = 0, J, K; I + LANs <= L; ++ I) {for (j = 0; j <LANs; ++ J) tans [J] = s [0] [I + J]; tans [J] = '\ 0'; For (k = 1; k <n; ++ K) if (! KMP (s [K], Tans) break; If (k = N) {If (strcmp (ANS, Tans)> 0) strcpy (ANS, Tans ); flag = 1 ;}}}if (flag = 1) printf ("% s \ n", ANS); 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.