POJ-3080---Blue Jeans (strstr函数暴力求解)
来源:互联网 发布:windows安装苹方字体 编辑:程序博客网 时间:2024/05/17 23:19
Blue Jeans
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 18274
Accepted: 8111
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.
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代码:
题意:在所给的字符串中找到公共的最长连续子串,长度相同则输出字典序小的,要注意长度小于3时输出no significant commonalities;
思路:由于数据范围很小,所以直接暴力枚举第一个字符串的所有子串,然后判断剩下的所有字符串中是否含有该字符串,找到最长的那一个。strstr函数可以判断一个字符串是否时另一个字符串的子串。
strstr函数用法:
strstr(str1,str2)用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。
(这个函数好像c++11后才支持,注意一下)
AC代码:
这题第一遍写的时候写成了if(strcmp(tmp,ans)),竟然过了!!然后发现了改过来又交了一遍也过了,醉醉的~#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;}
阅读全文
1 0
- POJ-3080---Blue Jeans (strstr函数暴力求解)
- POJ 3080 Blue Jeans(暴力)
- POJ 3080 Blue Jeans (暴力)
- POJ 3080 Blue Jeans (很暴力)
- POJ-3080 Blue Jeans(纯暴力)
- POJ 3080 Blue Jeans(暴力 + 串)
- poj 3080 Blue Jeans 暴力
- POJ 题目3080 Blue Jeans(KMP+暴力)
- POJ -3080 Blue Jeans(暴力就可以了)
- POJ 3080 Blue Jeans(暴力枚举+kmp)
- POJ 3080 Blue Jeans (KMP+暴力枚举)【模板】
- POJ 3080 Blue Jeans(暴力模拟+stl)
- [KMP或者暴力]POJ 3080 Blue Jeans
- POJ 3080 Blue Jeans 暴力枚举+KMP
- POJ 3080 Blue Jeans KMP+暴力
- poj 3080 Blue Jeans (kmp暴力)
- POJ 3080 Blue Jeans (KMP || 暴力)
- poj-3080-Blue Jeans-串-暴力
- /bin/bash: java: command not found 问题解决
- PHP获取指定日期是星期几的实现方法
- poj3177—Redundant Paths(缩点+边双联通分量)
- Android中attrs.xml文件的使用详解
- java 时间工具类 DateUtils.java DateFormat/Calendar/Timestamp
- POJ-3080---Blue Jeans (strstr函数暴力求解)
- 轮播图广告
- struts2运行机制
- hbase-1.2.4 集群 的 安装
- 【bzoj1133】[POI2009]Kon
- 京东的商品爬虫
- java图片上传后台实现以及删除时删除目录
- GCC编译过程
- iOS开发之键盘样式