POJ3038(Blue Jeans)
2015-08-17 19:27
501 查看
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
题意:给m (2 <= m <= 10) 个长60的字符串,求最长公共连续子串。
思路:对第一个串枚举起点和终点,分别得到连续的子串,然后分别和所有的剩下的m-1个串进行匹配,找到长度最大的,字典序靠前的连续子串。匹配用的是kmp算法。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14301 | Accepted: 6366 |
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
题意:给m (2 <= m <= 10) 个长60的字符串,求最长公共连续子串。
思路:对第一个串枚举起点和终点,分别得到连续的子串,然后分别和所有的剩下的m-1个串进行匹配,找到长度最大的,字典序靠前的连续子串。匹配用的是kmp算法。
#include<cstdio> #include<string.h> int t,n; char a[12][70]; char T[70],S[70]; int len, lenT; char ans[70]; int nxt[70]; void getnxt(){ int j = 0,k=-1; memset(nxt,0,sizeof(nxt)); nxt[0]=-1; while(j<len){ if(k==-1||T[j]==T[k]) nxt[++j]=++k; else k=nxt[k]; } } void kmp() { if(lenT<len) return; getnxt(); int tag=1; for(int m = 1; m < n; m++) { int j=0,k=0; while(k<lenT&&j<60) { if(k==-1||T[k]==a[m][j]) ++k,++j; else k=nxt[k]; } if(k!=lenT)tag=0; } if(tag&&lenT>=len) { if(lenT==len) { if(strcmp(ans,T)>0) for(int i = 0; i < len; i++)ans[i]=T[i]; } else for(int i = 0; i <lenT; i++)ans[i]=T[i],len=lenT; } } int main(){ scanf("%d",&t); while(t--){ len=0; scanf("%d",&n); for(int i = 0; i < n; i++) scanf("%s",a[i]); for(int i = 0; i < 58; i++){ for(int j = i+2; j < 60; j++){ for(int k = 0; k+i<= j; k++)T[k]=a[0][i+k]; lenT=j-i+1; kmp(); } } if(len==0) printf("no significant commonalities\n"); for(int i = 0; i < len; i++) if(i==len-1)printf("%c\n",ans[i]); else printf("%c",ans[i]); } }
相关文章推荐
- QuickSort 快速排序 基于伪代码实现
- easyui 快速开发整理
- iOS开发-Day25-UIView
- SPOJ 题目375 Query on a tree(link cut tree边权更新,求两点最大值)
- requireJs杂项
- String,StringBuffer和StringBuilder的区别
- poj 3368 Frequent values(一维RMQ)
- Chapter4: View - The Big Nerd Ranch Guide 读书笔记
- android build 过程 (ramdisk.img、sytem.img、userdata.img)和 启动过程简单分析
- UVA 11995 I Can Guess the Data Structure!【模拟】
- 使用query参数过滤组合AWS CLI输出信息
- UE4 Material Properties
- Android Studio Building 'xxx' Gradle project info
- UE3 材质概述
- EasyUI DataGrid 复选框
- 关于InvokeRequired与Invoke
- 解决Bui框架下拉框select配合Gird弹出模态编辑框使用,无法选取编辑行的下拉内容.
- POJ2533-Longest Ordered Subsequence
- Amazon UI 学习笔记
- 线段树专题—HDU 4027 Can you answer these queries?