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;
}


 




POJ3080-Blue Jeans

  • 2011年07月31日 00:01
  • 9KB
  • 下载

poj-3080 blue jeans

Description The Genographic Project is a research partnership between IBM and The National Geograp...

KMP+枚举 POJ-3080 Blue Jeans

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13232   Accepted: 5884...

POJ3080:Blue Jeans(后缀数组)

Description The Genographic Project is a research partnership between IBM and The National Geograph...

POJ 3080 Blue Jeans kmp+暴力枚举

D - Blue Jeans Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S...

poj 3080 Blue Jeans kmp 字符串匹配

题意:是否存在一个长度大于等于3的字符串是所有给出字符串的子串 题目思路:找出第一个字符串所有的子串,用kmp去匹配,因为字符串长度只有60,所以时间复杂度还是很理想的, o(60*60*m...

POJ 3080 Blue Jeans KMP

题意: 输入M个定长为60的DNA序列(字符串),求M条DNA的最长公共子序列,若有最长的公共子序列有若干条,则输出字典序最小的。 显然是一个字符串匹配问题。第一次用KMP算法。幸好看了严蔚敏老师的...
  • Tsaid
  • Tsaid
  • 2011年07月26日 21:58
  • 353

Blue Jeans(poj3080,后缀数组,求最长公共子串)

http://poj.org/problem?id=3080 http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=5414&pi...
  • JHC23
  • JHC23
  • 2013年08月14日 20:45
  • 738

POJ-3080 Blue Jeans(纯暴力)

C - Blue Jeans Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit...

POJ 3080 Blue Jeans(字符串处理函数)

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14113   Ac...
  • yeguxin
  • yeguxin
  • 2015年08月03日 10:36
  • 356
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 3080 Blue jeans
举报原因:
原因补充:

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