【原】 POJ 3080 Blue Jeans KMP 解题报告

 

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

 

方法:
将第一个字符串的所有子串枚举为pattern,匹配其他所有的字符串,找到与所有匹配的最大子串
匹配用KMP,注意到当不同pattern前缀相同时,预处理不必从头做起,因为每个next[i]之于其之前的next有关

 

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 could 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

GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

3

GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA

GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA

GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA

3

CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalities

AGATAC

CATCATCAT

 

   1: #include <stdio.h>
   2: #include <iostream>
   3: using namespace std ;
   4:  
   5: const int n = 60 ;
   6: char str[62] ;
   7: char pattern[62] ;  //pattern[1...60]
   8: char input[9][62] ; //input
   9:  
  10: int next[62] ;
  11:  
  12: void KMPPreprocess( int b, int m )
  13: {
  14:     int i,j ;
  15:  
  16:     j = next[b] ;
  17:     for( i=b+1 ; i<=m ; ++i )
  18:     {
  19:         while( j>0 && pattern[j+1]!=pattern[i] )
  20:             j = next[j] ;
  21:         if( pattern[j+1]==pattern[i] )
  22:             ++j ;
  23:         next[i] = j ;
  24:     }
  25: }
  26:  
  27: bool KMPMatch( int k, int m )  
  28: {
  29:     int i,j ;
  30:  
  31:     j = 0 ;
  32:     for( i=1 ; i<=60 ; ++i )
  33:     {
  34:         while( j>0 && pattern[j+1]!=input[k][i] )
  35:             j = next[j] ;
  36:         if( pattern[j+1] == input[k][i] )
  37:             ++j ;
  38:         if( j == m )
  39:             return true ;
  40:     }
  41:     return false ;
  42: }
  43:  
  44: void run3080()
  45: {
  46:     int num,cnt ;
  47:     int maxlen ;
  48:     int i,j,k,b,e ;
  49:     char maxstr[62] ;
  50:     char tmpstr[62] ;
  51:     bool allMatch ;
  52:  
  53:     scanf( "%d", &num ) ;
  54:     while( num-- )
  55:     {
  56:         memset( maxstr, 0, sizeof(maxstr) ) ;
  57:         memset( tmpstr, 0, sizeof(tmpstr) ) ;
  58:         memset( input, 0, sizeof(input) ) ;
  59:         memset( str, 0, sizeof(str) ) ;
  60:         memset( pattern, 0, sizeof(pattern) ) ;
  61:         memset( next, 0, sizeof(next) ) ;
  62:  
  63:         scanf( "%d%s", &cnt, str+1 ) ;
  64:         for( i=2 ; i<=cnt ; ++i )
  65:             scanf( "%s",(input[i-2])+1  ) ;
  66:         
  67:         maxlen = 2 ;
  68:         memset( maxstr, 0, sizeof(maxstr) ) ;
  69:         for( b=1 ; b<58 ; ++b )  //枚举第一个字符串的各子串
  70:         {
  71:             memset( next, 0, sizeof(next) ) ;
  72:             memset( pattern, 0, sizeof(pattern) ) ;
  73:             pattern[0] = '0' ;
  74:             strncpy( pattern+1, str+b, 2 ) ;  //以str[b]开头的pattern,其next可以不用每次都清空
  75:             next[1] = 0 ;
  76:             KMPPreprocess( 1, 2 ) ;
  77:  
  78:             for( j=3,e=b+2 ; e<=60 ; ++e,++j )  //j表示此时pattern长度
  79:             {
  80:                 allMatch = true ;
  81:                 strncat( pattern, str+e, 1 ) ;  //得到枚举的pattern
  82:                 
  83:                 k = next[j-1] ;                     //扩展新得到的字符对应的next
  84:                 while( k>0 && pattern[k+1]!=pattern[j] )
  85:                     k = next[k] ;
  86:                 if( pattern[k+1] == pattern[j] )
  87:                     ++k ;
  88:                 next[j] = k ;
  89:                 
  90:                 for( i=0 ; i<=cnt-2 ; ++i )  //将该pattern去和其他字符串做匹配,如果有一个不匹配则退出
  91:                 {
  92:                     if( !KMPMatch( i, j ) )
  93:                     {
  94:                         allMatch = false ;
  95:                         break ;
  96:                     }
  97:                 }
  98:  
  99:                 if( !allMatch )
 100:                     continue ;
 101:                 else if( j>maxlen )
 102:                 {
 103:                     maxlen = j ;
 104:                     strcpy( maxstr, pattern+1 ) ;
 105:                 }
 106:                 else if( j==maxlen && strcmp( maxstr, pattern+1 )>0 )
 107:                     strcpy( maxstr, pattern+1 ) ;
 108:             }
 109:         }
 110:         if( maxlen>=3 )
 111:             printf( "%s\n", maxstr ) ;
 112:         else
 113:             printf( "no significant commonalities\n" ) ;
 114:     }
 115: }

你可能感兴趣的:(poj)