关闭

poj 3080 Blue jeans

标签: 算法枚举 字符串poj
537人阅读 评论(0) 收藏 举报
分类:
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;
}


 




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:62770次
    • 积分:1219
    • 等级:
    • 排名:千里之外
    • 原创:62篇
    • 转载:0篇
    • 译文:0篇
    • 评论:19条
    博客专栏
    最新评论
    cocos2dx
    cocos2dx c++ lua 学习记录