加载中...

POJ 3080 - Blue Jeans


0x00 POJ 原题

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

0x10 题目概述

就是求 k 个长度为 60 的字符串的最长连续公共子串, 2<=k<=10

规定:

  • 最长公共串长度小于 3 不输出
  • 若出现等长的最长的子串,则输出字典序最小的串

0x20 解题思路

LCS 问题,纠结了几个月放着没做的题目。。

一直以为要用 KMP 或者后缀数组来做。。。

然后我就拼命学后缀。。。

今天偶然发现直接 暴力 能够达到 0ms 的效果= =

所以。。。暴力吧。。。不愧为初级的题。。。

暴力思想很简单:

开二维 DNA[][] 保存所有 DNA 序列

  1. 以 DNA[0] 为母版,顺次截取 60 个长度 length=1 的子串 dna[],检查其他 DNA[i] 是否都有子串 dna,若是则把 dna[] 复制到 obj[],否则枚举下一个长度 length 的子串;若 obj 与新的 dna 等长,则比较两者字典序,当 dna 字典序小时,复制到 obj

  2. 第 1 步循环 60 次后 length+1。

    顺次截取 59 个长度 length=2 的子串 dna[],重复 1 的操作更新 obj。。

  3. 第 2 步循环 59 次后 length+1。

    顺次截取 58 个长度 length=3 的子串 dna[],继续。。

  • ………..
  1. 第 59 步循环 2 次后 length+1。

    顺次截取 1 个长度 length=60 的子串 dna[],继续重复操作更新 obj。。

  2. 输出 obj

0x30 测试数据

0x40 AC 源码

//Memory Time 
//248K   16MS 

#include<iostream>
#include<string.h>
using namespace std;

const int len=60;

int main(int i,int j)
{
    int test;
    cin>>test;
    for(int t=1;t<=test;t++)
    {
        int n;  //DNA个数
        cin>>n;
        char** DNA=new char*[n];
        for(int p=0;p<n;p++)
        {
            DNA[p]=new char[len+1];
            cin>>DNA[p];
        }

        char obj[len+1];  //所有DNA的公共串
        int StrLen=0;    //最长公共串长度
        int length=1;    //当前枚举的公共串长度

        for(i=0;;i++)  //枚举公共串dna[]
        {
            /*截取DNA[0][]中以pi为起点,长度为length的子串dna[]*/
            char dna[len+1];
            int pi=i;
            if(pi+length>len)
            {
                length++;
                i=-1;
                if(length>len)
                    break;
                continue;
            }
            for(j=0;j<length;j++)
                dna[j]=DNA[0][pi++];
            dna[j]='\0';

            /*检查其他DNA[][]是否都存在字符串dna[]*/
            bool flag=true;
            for(int k=1;k<n;k++)
                if(!strstr(DNA[k],dna))  //存在一个DNA不含有dna[]
                {
                    flag=false;
                    break;
                }

            /*确认最大公共串*/
            if(flag)
            {
                if(StrLen<length)
                {
                    StrLen=length;
                    strcpy(obj,dna);    
                }
                else if(StrLen==length)
                {
                    if(strcmp(obj,dna)>0)  //存在相同长度的公共串时,取最小字典序的串
                        strcpy(obj,dna);
                }
            }
        }

        if(StrLen<3)
            cout<<"no significant commonalities"<<endl;
        else
            cout<<obj<<endl;

        delete DNA;
    }
    return 0;
}

相关资料


文章作者: EXP
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 EXP !
赏
  目录