POJ 3080 Blue Jeans (BFS或暴力枚举)

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

题意是求N个字符串的字典序最小的最大公共子串。

第一种方法是暴力枚举,先把第一串的所有情况枚举出来,再与后面所有字符串进行比较。

代码如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

char str[15][65];
char sub[65];
char ans[65];
int len;

int main()
{
    int t,i,j,k,n,maxn;
    scanf("%d",&t);
    while(t--)
    {
        maxn = 0;
        scanf("%d",&n);
        memset(ans,'\0',sizeof(ans));
        for(i = 1; i<=n; i++)
            scanf("%s",str[i]);
        for(i = 1; i<=60; i++)
        {
            int find = 0;
            for(j = 0; j<=60-i; j++)
            {
                len = 0;
                int check = 1;
                for(k = j;;k++)
                {
                    sub[len++] = str[1][k];//sub取str[1]的所有情况的子串
                    if(len == i)
                        break;
                }
                sub[len] = '\0';
                for(k = 2; k<=n; k++)
                {
                    if(!strstr(str[k],sub))//判断sub是否为str[k]的子串,是则返回首地址,不是则返回NULL
                    {
                        check = 0;//若sub有一个不是str[i]的子串,则退出。证明该str[1]的子串不是要的答案
                        break;
                    }
                }
                if(check)//若找到了公共子串,筛选出最短的 ,若同样短,选出字典序最小的
                {
                    find = 1;
                    if(strlen(ans)<strlen(sub)) //长度筛选
                    strcpy(ans,sub);
                    else if(strcmp(ans,sub)>0)//字典序筛选
                    strcpy(ans,sub);
                }
            }
            if(!find)
            break;
        }
        if(strlen(ans)<3)
            printf("no significant commonalities\n");
        else
            printf("%s\n",ans);
    }

    return 0;
}
第二种方法是用KMP来求解。
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
const int Max = 62;

char text[10][Max], pat[Max];
int n, ma, lenp, next[Max];

void get_next()  //求出KMP的NEXT数组
{
    int i = 0, j = -1;
    next[0] = -1;
    while(i < lenp)  //lenp代表pat的长度
    {
        if(j == -1 || pat[i] == pat[j])
        {
            i ++; j ++;
            next[i] = j;
        }
        else j = next[j];
    }
}

void KMP()  //求出pat串与所有串的最长值,看是否有最大公共子串
{
    int k, m, i, j;
    get_next();
    ma = 100;  //代表公共子串的最大长度值
    for(k = 1; k < n; k ++)
    {
        i = 0; j = 0; m = 0;
        while(i < 60 && j < lenp)  //i指向被比较的text串,j指向pat串
        {
            if(j == -1 || text[k][i] == pat[j])
            {
                i ++; j ++;  //没有next[i] = j;  这一步了
            }
            else j = next[j];
            if(j > m) m = j;  //如果找到相同的子串,则标记公共子串长度
        }
        if(m < ma) ma = m;
    }
}


int main()
{
    int t, i;
    char result[Max];
    scanf("%d", &t);
    while(t --)
    {
        scanf("%d", &n);
        for(i = 0; i < n; i ++)
            scanf("%s", text[i]);
        int ans = 0;
        for(i = 0; i <= 57; i ++)
        {
            strcpy(pat, text[0] + i);    //  枚举第一个串的所有后缀串,从text[0]+i到NULL(包括)(当然最后2个可以省去)。
            //cout<<pat<<endl;
            lenp = 60 - i;  //lenp代表pat的长度
            KMP();                       //  KMP求出这个后缀串与其余所有串的最大匹配。
            if(ans < ma)
            {
                ans = ma;
                strncpy(result, text[0] + i, ans);
                result[ans] = '\0';
            }
            else if(ans == ma)
            {         //  存在多个最长公共子串,输出字典序最小的,WA了一次。

                char tmp[Max];
                strncpy(tmp, text[0] + i, ans);    //  复习: strncpy()没有复制最后的'\0'。
                tmp[ans] = '\0';
                if(strcmp(tmp, result) < 0)
                strcpy(result, tmp);
            }
        }
        if(ans >= 3)
            printf("%s\n", result);
        else
            printf("no significant commonalities\n");
    }
    return 0;
}





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值