poj3080 (KMP or 直接暴力)

题目链接:

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

Blue Jeans

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21868 Accepted: 9707

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

第一种: 直接暴力:

This is the code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include <iomanip>
#include<list>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
string s[11];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int m;
        string str="no significant commonalities";
        cin>>m;
        for(int i=1;i<=m;++i)
            cin>>s[i];
        for(int k=3;k<=60;++k)//长度
        {
            for(int i=0;i<=60-k;++i)//从第几个开始的
            {
                string ss=s[1].substr(i,k);
                //for(int j=i;i<=k;++j)//取k长度的数
                   // ss=ss+s[1][j];
                //cout<<ss<<endl;
                bool falg=true;
                for(int j=2;j<=m;++j)
                {
                    if(s[j].find(ss)==string::npos)
                    {
                        falg=false;
                        break;
                    }
                }
                if(!falg)
                   continue;
                else
                {
                    if(str=="no significant commonalities")
                        str=ss;
                    else if(str.size()<ss.size())
                        str=ss;
                    else if(str.size()==ss.size())
                        str=min(str,ss);
                }
            }
        }
        cout<<str<<endl;
    }
}

第二种:KMP

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
    int ret=0, flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        ret = ch - '0';
    while((ch=getchar())>='0'&&ch<='9')
        ret=ret*10+(ch-'0');
    return flag ? -ret : ret;
}

char s[10][1010];//待匹配串
char s2[1010];
char str[1010];//输出
int Next[1010];//优化后的失配指针,记住这里f要比P多一位,因为P到m-1即可,但是f还要计算出m的失配指针
int Next2[1010];//Next2用来保存KM指针,是为优化f的失配指针,f保存的是优化之后的失配指针
int ma;
int n;
void getFail(int len)
{
    Next[0]=Next[1] = 0;
    Next2[0]=Next2[1]=0;
    for(int i=1; i<len; i++)
    {
        int j=Next2[i];
        while(j&&s2[i]!=s2[j] )
            j=Next2[j];
        Next2[i+1]=Next[i+1]=(s2[i]==s2[j]) ? j+1 : 0;
        //既然i+1的失配位置指向j+1,但是P[i+1]和P[j+1]的内容是相同的
        //所以就算指针从i+1跳到j+1去,还是不能匹配,所以Next[i+1]直接=Next[j+1]
        if(Next[i+1]==j+1 && s2[i+1]==s2[j+1])
            Next[i+1]=Next[j+1];
    }
}
void Find(int x,int len) //找到所有匹配点
{
    getFail(len);
    ma=100;
    for(int k=1;k<n;++k)
    {
        int j=0;
        int m=0;
        for(int i=0; i<x&&j<len; i++)
        {
            while(j && s[k][i]!=s2[j])
                j=Next[j];
            if(s[k][i]==s2[j])
                j++;
            if(j>m)//寻找长的
                m=j;
        }
        if(m<ma)
            ma=m;//只有小的能包含长的
    }
}
int main()
{
    //freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);
    //freopen("D:\\chnegxubianji\\inORout\\out.txt", "w", stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%s",s[i]);
        int anslen=0;//最终长度
        for(int i=0; i<=57; i++)
        {
            strcpy(s2,s[0]+i);//
            int len=60-i;
            Find(60,len);
            if(ma>anslen)//找到长度大于之前的
            {
                anslen=ma;
                strncpy(str,s[0]+i,ma);
                str[anslen]='\0';
            }
            else if(ma==anslen)//相同长度寻找字典序小的
            {
                strncpy(s2,s[0]+i,anslen);
                s2[anslen]='\0';
                if(strcmp(s2,str)<0)
                {
                    strcpy(str,s2);
                    str[anslen]='\0';
                }
            }
        }
        if(anslen>=3)
            printf("%s\n",str);
        else
            puts("no significant commonalities");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值