Blue Jeans POJ - 3080

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 ©. 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

这个题主要用的是KMP算法
我会敲KMP算法却还没完全理解它,有些难受
KMP+暴力枚举
我的枚举是从最大的枚举下去的
只要每次枚举的时候对字符串进行一些小修改,让字符串的长度符合当前检查的长度就好
完事以后,每一次进行下一次check的时候,要对这个字符串进行还原

#include <iostream>
#include <stdio.h>
#include <cstring>

using std::cout;
using std::endl;
using std::cin;

const int length = 60;
class KMP
{
    char* f,*s;
    int *next;
    int* getNext(char *);
public:
    KMP(char *p){s = p;}
    void initializer(){next = getNext(s);}
    ~KMP(){}
    int kmp();
    void setF(char* p){f = p;}
    void setS(char* p){s = p;}
};
int main()
{
    int dataset;
    cin >> dataset;
    char* p[10],s[61];
    //KMP find;
    while(dataset--)
    {
        int n;
        cin >> n;
        for(int i = 0;i < n;i++)
        {
            char *temp = new char[61];
            scanf("%s",temp);
            p[i] = temp;
        }
        strcpy(s,p[0]);
        KMP check(p[0]);
        check.initializer();
        int j0[length];
        int is = 1;
        for(int i = length; i >= 3;i--)//外面两层循环是为了遍历第一个字符串所有长度大于3的子串
        {//i是子串长度

            int counter = 0;int flag = 1;
            for(int j = 0;j <= length - i;j++)
            {//j是子串开始的位置
                flag = 1;
                p[0][j + i] = 0;
                check.setS(p[0] + j);
                for(int k = 1;k < n;k++)//遍历下面的所有字串,如果有不匹配的就可以直接跳出,该轮有问题
                {//k是下面字符串的下标
                    check.setF(p[k]);
                    check.initializer();
                    int temp = check.kmp();
                    if(temp == -1)
                    {
                        flag = 0;
                        break;
                    }
                }
                if(flag)
                    j0[counter++] = j;
                strcpy(p[0],s);
            }

            if(counter)//如果这个循环(长度)内有符合要求的字段,就找到最大的字段输出
            {
                is = 0;
                int min = j0[0];
                for(int k = 0;k < counter;k++)
                {
                    if(p[0][j0[k]] < p[0][min])
                        min = j0[k];
                }
                p[0][min + i] = 0;
                printf("%s\n",p[0] + min);
                break;
            }

        }
        if(is)
        {
            cout << "no significant commonalities" << endl;
        }

        for(int i = 0;i < n;i++)
            delete [] p[i];
    }
    return 0;
}
int KMP::kmp()
{
    int len1 = strlen(f);
    int len2 = strlen(s);
    int i = 0,j = 0;
    while(i < len1 && j < len2)
    {
        if(j == -1 || f[i] == s[j])
        {
            i++;
            j++;
        }
        else
            j = next[j];
    }
    if(j == len2)
        return i - j;
    else
        return -1;
}
int* KMP::getNext(char* p)
{
    int len = strlen(p);
    int *next = new int[len];
    next[0] = -1;
    int i = 0,j = -1;
    while(i < len)
    {
        if(j == -1 || p[i] == p[j])
        {
            j++;
            i++;
            next[i] = j;
        }
        else
            j = next[j];
    }
    return next;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值