poj 3080 Blue Jeans 【KMP 暴力枚举】
2015-08-17 21:17
615 查看
Blue Jeans
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14307 | Accepted: 6368 |
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个串,让你找出最长公共子串(长度必须大于2)。若有长度相同的子串,输出字典序较小的。 无语死了,一直以为考察后缀数组,没想到KMP暴力也可以过。 1A之后,好后悔自己没有去尝试。 思路:枚举一个串的所有子串,判断该子串有没有在其它串中出现。若在其它串中全部出现 则更新子串,否则枚举下一个子串。 这周把后缀数组看了 fighting ! AC代码:#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char str[10][70]; int f[70]; void getfail(char *s) { int len = strlen(s); f[0] = f[1] = 0; for(int i = 1; i < len; i++) { int j = f[i]; while(j && s[i] != s[j]) j = f[j]; f[i+1] = s[i]==s[j] ? j+1 : 0; } } bool find(char *a, char *b)//判断 b串在不在a串 { int la = strlen(a); int lb = strlen(b); int j = 0; for(int i = 0; i < la; i++) { while(j && a[i] != b[j]) j = f[j]; if(a[i] == b[j]) j++; if(j >= lb) return true; } return false; } int main() { int t, N; scanf("%d", &t); while(t--) { scanf("%d", &N); for(int i = 0; i < N; i++) scanf("%s", str[i]); int l = strlen(str[0]); char ss[70];//枚举 匹配的串 char ans[70];//结果 串 memset(ans, '\0', sizeof(ans)); for(int i = 0; i < l-2; i++)//枚举串的起点 { for(int j = i+2; j < l; j++)//枚举串的终点 { int p = 0; for(int k = i; k <= j; k++)//存储新子串 ss[p++] = str[0][k]; ss[p] = '\0'; getfail(ss); bool flag = true; for(int k = 1; k < N; k++) { if(!find(str[k], ss)) { flag = false;//失败 break; } } if(flag)//成功 更新 { if(strlen(ss) > strlen(ans))//比较长度 strcpy(ans, ss); else if(strlen(ss) == strlen(ans))//比较字典序 { if(strcmp(ss, ans) < 0) strcpy(ans, ss); } } } } if(strlen(ans) == 0) printf("no significant commonalities\n"); else printf("%s\n", ans); } return 0; }
相关文章推荐
- UI_model传值, json数据解析
- LeetCode之Unique Binary Search Trees II
- hdu 3836 Equivalent Sets(tarjan+缩点)
- UITableView之上拉刷新
- IOS UITableView 图片文字重叠问题
- POJ 题目3080 Blue Jeans(KMP+暴力)
- Powerbuilder 连接字符串各个参数 Database parameters and supported database interfaces
- UIView添加手势 然后UITableView 添加进这个View 导致UITableView 的单元格点击事件无效
- uva 1342 - That Nice Euler Circuit(欧拉定理)
- 1085. Perfect Sequence (25)
- Squence 设置主键自动增长,设置起始值、步长
- Quick Reference: git 的.gitignore文件
- [POJ2478]Farey Sequence
- UIMenuController 的使用指南
- HDU2227(Find the nondecreasing subsequences)
- 对UITableView的一些设置
- Light OJ 1188 Fast Queries(分块暴力)
- POJ3038(Blue Jeans)
- QuickSort 快速排序 基于伪代码实现
- easyui 快速开发整理