poj 3080 Blue jeans

原创 2013年12月05日 10:15:09
Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10618   Accepted: 4537

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



大致题意:给定n组测试数据每组有m个字符串(长度为60),找出这些字符串的最长公共子串,如果有多个长度相等的公共子串,输出按字典序出现的第一个子串。

解题思路:因为数据量并不是很大,采用暴力枚举就可以AC,具体实现:首先枚举其中一个字符串的子串

                (从最长子串枚举),再在其他字符串中查找,如果都包含且是第一次出现此长度的子串,先保存起来,

                 继续查找此长度的子串,如果再次出现与上次找到的比较(用strcmp)判断字典序大小,并保存小的

                 重复上述步骤,知道遍历完此长度的所有子串,最后输出结果。

提示:我并没有按固定长度60,因为以前做的一个题与这个题类似,但字符串是不定长的,如果这个题也改成

           不定长的,应该更有意思。

代码:

#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
 
string str[11], sz;
int main()
{
    int n, m, i, len;
    scanf("%d", &n);
    while (n--)
    {
        scanf("%d", &m);
        len = 0xfffffff;
        getchar();
        int idx;
        for (i=0; i<m; ++i)
        {
            getline(cin, str[i]);
            if (str[i].length() < len)
            {
                idx = i;
                len = str[i].length();
            }
        }
         
        int j, k, flag2 = 0, flag1;
        char f[200], tmp[200];
         
        for (j=len; j>=3; --j)
        {
            for (k=0; k<=len-j; ++k)
            {
                flag1 = 1;
                sz = str[idx].substr(k, j);
                for (i=0; i<m; ++i)
                {
                    if (i == idx)
                        continue;
                    if (str[i].find(sz) != -1)
                        strcpy(tmp, sz.c_str());
                    else
                    {
                        flag1 = 0;
                        break;
                    }
                }
                if (flag1)
                {
                    if (flag2)
                    {
                        if (strcmp(tmp, f) < 0)
                            strcpy(f, tmp);
                    }
                    else
                        strcpy(f, tmp);
                    flag2 = 1;
                }
            }
            if (flag2)
                break;
        }
 
        if (flag2)
            puts(f);
        else
            puts("no significant commonalities");
    }
    return 0;
}


 




POJ - 3080 Blue Jeans解题报告(kmp)

题目大意: 给你好几串字符串(最多10串,每串最多60个字符),让你找出他们最长的公共子串。...
  • qq_36306833
  • qq_36306833
  • 2017年02月16日 15:21
  • 173

POJ3080:Blue Jeans

Description The Genographic Project is a research partnership between IBM and The National Geograph...
  • libin56842
  • libin56842
  • 2013年12月08日 20:04
  • 3611

poj-3080 Blue Jeans

Description The Genographic Project is a research partnership between IBM and The National Geograph...
  • chAngE_AC
  • chAngE_AC
  • 2015年08月10日 19:10
  • 410

POJ 3080 Blue Jeans (很暴力)

题目链接:http://poj.org/problem?id=3080 Blue Jeans Time Limit: 1000MS   Memory Limit: 6553...
  • hkh746392783
  • hkh746392783
  • 2016年07月24日 19:09
  • 248

POJ 3080 Blue Jeans 笔记

已知 m 条DNA字符串,求最长的公共连续字符串。
  • woniupengpeng
  • woniupengpeng
  • 2017年07月19日 21:22
  • 64

POJ 3080 Blue Jeans 三种暴力法

本题可以使用暴力法直接求解,思路也挺简单的,不过实现起来也挺麻烦的。 本题最暴力直接使用strstr过。 这里使用hash表的方法过,这种方法好像有个学名的,主要思路就是把一个需要查找的字符串赋予一...
  • kenden23
  • kenden23
  • 2014年08月03日 13:07
  • 911

poj 3080 Blue Jeans

点击打开链接 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: ...
  • a27985649
  • a27985649
  • 2013年08月28日 19:26
  • 470

POJ 3080 Blue jeans

KMP求解最长公共子序列问题
  • ChiLuManXi
  • ChiLuManXi
  • 2015年11月27日 12:31
  • 449

poj 3080 Blue Jeans

点击打开链接poj 3080 思路:kmp+子串枚举 分析: 1 题目要求的是给定m个DNA序列,每个DNA序列长度固定为60,问m个DNA序列的最长的公共子串 2 初看题目无从下...
  • cgl1079743846
  • cgl1079743846
  • 2012年10月29日 20:17
  • 949

Blue Jeans POJ - 3080

The Genographic Project is a research partnership between IBM and The National Geographic Society th...
  • islittlehappy
  • islittlehappy
  • 2018年02月01日 14:22
  • 41
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 3080 Blue jeans
举报原因:
原因补充:

(最多只允许输入30个字)