POJ-3080---Blue Jeans (strstr函数暴力求解)

Blue Jeans
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 18274
Accepted: 8111

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


题意:在所给的字符串中找到公共的最长连续子串,长度相同则输出字典序小的,要注意长度小于3时输出no significant commonalities;
思路:由于数据范围很小,所以直接暴力枚举第一个字符串的所有子串,然后判断剩下的所有字符串中是否含有该字符串,找到最长的那一个。strstr函数可以判断一个字符串是否时另一个字符串的子串。

strstr函数用法:
strstr(str1,str2)用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。
(这个函数好像c++11后才支持,注意一下)

AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int t,k;
char str[15][66];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&k);
        for(int i=1;i<=k;i++)
            scanf("%s",str[i]);
        char ans[66]="\0";
        for(int i=0;i<59;i++)
        {
            for(int j=i+1;j<60;j++)//枚举str[1]的所有子串
            {
                char tmp[66]="\0";
                int cnt=0;
                for(int v=i;v<=j;v++)
                    tmp[cnt++]=str[1][v];
                int flag=1;
                for(int v=2;v<=k;v++)
                {
                    if(strstr(str[v],tmp)==NULL)
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag)//tmp为剩下所有字符串的子串则进行判断
                {
                    if(strlen(tmp)>strlen(ans))
                        strcpy(ans,tmp);
                    else if(strlen(tmp)==strlen(ans))
                    {
                        if(strcmp(ans,tmp))
                            strcpy(ans,tmp);
                    }
                }
            }
        }
        if(strlen(ans)<3)
            printf("no significant commonalities\n");
        else
            printf("%s\n",ans);
    }
    return 0;
}


这题第一遍写的时候写成了if(strcmp(tmp,ans)),竟然过了!!然后发现了改过来又交了一遍也过了,醉醉的~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值