您的位置:首页 > 产品设计 > UI/UE

poj 3080 Blue Jeans KMP模式匹配

2016-01-25 13:22 567 查看
给出m个长度为60的字符串,现在要你找到最长公共子串(若多个,取字典序最小)

Blue Jeans

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15284 Accepted: 6786
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

Source

South Central USA 2006

解法:

首先要从n个中拿出一个作“缝衣针”M。

最长公共子串一定在M里面。

看了网上的题解,有的说要枚举m的所有子串,其实没必要,

只用枚举60个子串M[i,59] (i from 0 to 59);

对于每一个字符串i,dp[i][pos]表示缝衣针 在和字符串i匹配时以位置pos结尾时最大匹配长度。

dp[0][pos]=min{     dp[i][pos]      },表示缝衣针和所有字符串都能匹配时,pos位置结尾 能匹配的最大长度。

 充分利用了KMP匹配时缝衣针指针p指向的是正在匹配字符所能配到的最大位置。

/**==========================================
*   This is a solution for ACM/ICPC problem
*
*   @source:poj 3080 Blue Jeans
*   @type:  data structure-KMP
*   @author: wust_ysk
*   @blog:  http://blog.csdn.net/yskyskyer123 *   @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn= 60   ;
string N[12],M,S;
int dp[12][maxn+4],nex[maxn+4];
int n;
const int len=60;

void getnex(int len_M)
{
nex[0]=nex[1]=0;
for(int i=1;i<len_M;i++)
{
int p=nex[i];
while(p&&M[p]!=M[i])  p=nex[p];
nex[i+1]= M[p]==M[i]?p+1 :0;
}

}

void KMP(int ind,int len_M)
{

int p=0;
int delta=len-len_M;
for(int i=0;i<len;i++)
{
while(p&&M[p]!=N[ind][i])  p=nex[p];

if(M[p]==N[ind][i])
{
dp[ind][p+delta]=max(dp[ind][p+delta],p+1 );
p++;
}

if(p==len_M)
{
p=nex[p];
}

}
}

void work()
{
memset(dp,0,sizeof dp);
for(int i=0;i<len;i++)
{
M=S.substr(i,len);
getnex(len-i);
for(int j=1;j<=n;j++)
{

KMP(j,len-i);
}
}
int maxi=0;
string t="";

memset(dp[0],0x3f,sizeof dp[0]);
for(int p=0;p<len;p++)
{
for(int i=1;i<=n;i++)
{
dp[0][p]=min(dp[0][p],dp[i][p]);
}
}

for(int i=0;i<len;i++)
{
if(dp[0][i]>maxi)
{
maxi=dp[0][i];
t="";
int st=i-dp[0][i]+1;
for(int j=st;j<=i;j++)
{
t+=S[j];
}

}
else if(dp[0][i]==maxi)
{
string tmp="";
int st=i-dp[0][i]+1;
for(int j=st;j<=i;j++)
{
tmp+=S[j];
}
t=min(tmp,t);
}
}
if(maxi<3)  cout<<"no significant commonalities"<<endl;
else cout<<t<<endl;

}

int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n;
cin>>S;
n--;
for(int i=1;i<=n;i++)
{
cin>>N[i];
}
work();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: