博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3080 Blue Jeans【KMP】【暴力】
阅读量:4987 次
发布时间:2019-06-12

本文共 4152 字,大约阅读时间需要 13 分钟。

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:21746   Accepted: 9653

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

 

题意:

给定$m$个场长度为$60$的字符串,问他们的最长公共子串。

思路:

因为$m$和长度都很小,所以可以暴力枚举一个串的所有子串,$KMP$进行匹配。

 等之后【后缀数组】刷熟练一点了再用后缀数组做一次。奇怪的是hdu2328明明是一样的题意,怎么就总是WA

1 #include
2 //#include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 using namespace std; 14 typedef long long LL; 15 typedef unsigned long long ull; 16 #define pi 3.1415926535 17 #define inf 0x3f3f3f3f 18 19 const int maxn = 65; 20 int n, m; 21 char str[15][maxn]; 22 int nxt[maxn]; 23 24 void getnxt(char *s) 25 { 26 int len = strlen(s); 27 nxt[0] = -1; 28 int k = -1; 29 int j = 0; 30 while(j < len){ 31 if(k == -1 || s[j] == s[k]){ 32 ++k;++j; 33 if(s[j] != s[k]){ 34 nxt[j] = k; 35 } 36 else{ 37 nxt[j] = nxt[k]; 38 } 39 } 40 else{ 41 k = nxt[k]; 42 } 43 } 44 } 45 46 bool kmp(char *s, char *t) 47 { 48 getnxt(s); 49 int slen = strlen(s), tlen = strlen(t); 50 int i = 0, j = 0; 51 while(i < slen && j < tlen){ 52 if(j == -1 || s[i] == t[j]){ 53 j++; 54 i++; 55 } 56 else{ 57 j = nxt[j]; 58 } 59 } 60 if(j == tlen){ 61 return true; 62 } 63 else{ 64 return false; 65 } 66 } 67 68 int main() 69 { 70 scanf("%d", &n); 71 while(n--){ 72 scanf("%d", &m); 73 for(int i = 0; i < m; i++){ 74 scanf("%s", str[i]); 75 } 76 77 char ans[maxn]; 78 int anslen = -1; 79 for(int i = 0; i < 60; i++){ 80 for(int j = i; j < 60; j++){ 81 char t[maxn]; 82 memcpy(t, str[0] + i, j - i + 1); 83 t[j - i + 1] = '\0'; 84 bool flag = true; 85 for(int k = 1; k < m; k++){ 86 if(!kmp(str[k], t)){ 87 flag = false; 88 break; 89 } 90 } 91 if(flag){ 92 if(j - i + 1 > anslen){ 93 anslen = j - i + 1; 94 strcpy(ans, t); 95 } 96 else if(j - i + 1 == anslen){ 97 if(strcmp(ans, t) > 0){ 98 strcpy(ans, t); 99 }100 }101 }102 else{103 break;104 }105 }106 }107 108 if(anslen < 3){109 printf("no significant commonalities\n");110 }111 else{112 //cout<
<

 

转载于:https://www.cnblogs.com/wyboooo/p/10034142.html

你可能感兴趣的文章
Linux下的crontab定时执行任务命令详解
查看>>
【Java POI】POI基于事件驱动解析大数据量2007版本Excel,空值导致列错位问题
查看>>
.Net_05_事务的基本语法(Sql 语句)
查看>>
c++ 获取某个进程个数
查看>>
SparkSQL相关语句总结
查看>>
[洛谷P1514]引水入城
查看>>
[NC189A]数字权重
查看>>
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
查看>>
VS2015和OpenCV配置
查看>>
PHP_D4_“简易聊天室 ”的具体技术实现
查看>>
[BAT]通过schtasks.exe远程调用windows 2008 server上的计划任务,提示ERROR : Access is denied...
查看>>
关于实习的一些问题
查看>>
Mybatis Insert、update、delete流程
查看>>
NameValueCollection与Hashtable的区别
查看>>
DevExpress XtraReports 入门三 创建 Master-Detail(主/从) 报表
查看>>
Json对象的深浅拷贝
查看>>
HDOJ-1013
查看>>
A. Bus to Udayland
查看>>
Python 基础篇:字典、集合、文件操作
查看>>
Jmail发送邮件与带附件乱码解决办法
查看>>