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

来源:互联网 发布:windows安装苹方字体 编辑:程序博客网 时间:2024/05/17 23:19
Blue Jeans
Time Limit: 1000MS
Memory Limit: 65536KTotal 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

32GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA3GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAGATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAAGATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA3CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalitiesAGATACCATCATCAT

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)),竟然过了!!然后发现了改过来又交了一遍也过了,醉醉的~

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 b站页面卡顿怎么办 被全景视觉骗了怎么办? 逆战进去没声音怎么办 Mac电脑ai卡住了怎么办 pr字幕打不了字怎么办 pr手写字多笔画怎么办 家中挖矿噪音很大怎么办 学了栏目包装不想干怎么办 在文本打字乱了怎么办 体考后觉得成绩有出入怎么办 荧光棒进眼睛里怎么办 无法读源文件或磁盘怎么办 爱剪辑视频横着怎么办 pr界面字体太小怎么办 0p手机卡槽坏了怎么办 pr视频有水波纹怎么办 手机耳机插口插上没声音怎么办 xp系统电脑没有声音怎么办 win10笔记本耳机没声音怎么办 电脑插耳机还是外放怎么办 主机电脑耳机插孔坏了怎么办 苹果6plus有点卡怎么办 手机拨通电话后黑屏怎么办 手机拨通后没声音怎么办 苹果手机铃声不响了怎么办 婴儿喘气粗好像有痰怎么办 金毛呼吸声很大怎么办 回看福建导视该怎么办 楼上木地板走路声音都很响怎么办 班里团支不搭理我我该怎么办 客户买家具说贵怎么办 英雄联盟超出显示范围怎么办 脸上有斑点怎么办内条 脖子扭到了一动就疼怎么办 杭州限行在路上怎么办 在路上限行了怎么办 微信号不能进群怎么办 在微信约妹子被骗2000怎么办 qq号进不了热聊怎么办 网上买彩票中五百万怎么办 别人登我支付宝怎么办