Algorithm title: POJ 3080 Blue jeans (KMP matching, enumeration substring)

Source: Internet
Author: User
Tags bool strlen

Link:

http://poj.org/problem?id=3080

The main effect of the topic:

A DNA sequence of M (2<=m<=10) is given to find the longest common string in this m sequence. If there are multiple identical lengths, the output dictionary order is minimal.

Analysis and Summary:

Enumerate all the substrings in turn, and then see if they can match in all the sequences. Save the sequence with the largest length and the smallest dictionary order.

Code:

#include &lt;iostream&gt; #include &lt;cstdio&gt; #include &lt;cstring&gt; using namespace std;  
const int MAXN = 1000005;  
const int N = 60;  
int Nmax;  
Char seq[12][85];  
Char ans[85];  
    
int f[85];  
    void Getfail (char *p, int *f) {int m=strlen (p);  
    f[0]=f[1]=0;  
        for (int i=1; i&lt;m; ++i) {int j=f[i];  
        while (J &amp;&amp; P[i]!=p[j]) j=f[j];  
    F[I+1] = p[i]==p[j]?1+j:0;  
    } bool Find (char *t,char *p,int *f) {getfail (p,f);  
    int N=strlen (T);  
    int M=strlen (p);  
    int j=0;  
         for (int i=0; i&lt;n; ++i) {while (J &amp;&amp; T[i]!=p[j]) j=f[j];  
         if (T[i]==p[j]) ++j;  
         if (j==m) {return true;  
return false;  
    int main () {int ncase, m;  
    Char str[85];  
    scanf ("%d", &amp;ncase);  
        while (ncase--) {scanf ("%d", &amp;m);  
        for (int i=0; i&lt;m; ++i) {scanf ("%s", Seq[i]);  
   } 
        BOOL Ok=false;  
        nmax=3; Enumerate all substrings for (int i=0; i&lt;m; ++i) {for (int j=0; j&lt;n-3; ++j) {//enum start Me  
                Mset (str, 0, sizeof (str));  
    
                    for (int k=j,p=0; k&lt;n; ++k) {str[p++]=seq[i][k];  
                        if (P&gt;=nmax) {bool Flag=false;  
                                for (int l=0; l&lt;m; ++l) if (l!=i) {if (!find (seq[l],str,f)) {  
                                Flag=true;  
                            Break  
                                } if (!flag) {if (P==nmax) {  
                                    if (!ok) {ok=true;  
                                strcpy (ANS,STR);  
                               else if (strcmp (ANS,STR) &gt;0) {     strcpy (ANS,STR); } else if (P&gt;nmax) {Nmax  
                                =p;  
                            strcpy (ANS,STR);  } else{break;  
        If this length does not match, then even longer all cannot match}}}  
        if (OK) puts (ans);  
    Else puts ("no significant commonalities");  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.