poj3080Blue Jeans(在m个串中找到这m个串的 最长连续公共子序列)
来源:互联网 发布:centos的iso文件 编辑:程序博客网 时间:2024/06/05 10:00
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
题目意思是:找出m个串中最长连续公共串,串长度要大于等于3,并且当最长公共串有多个,则输出字典序最小.
解析:这里假设有3个串,长度设短一点为8,程序只有16MS
1:ABCDAFKD 2:BCDAKJSA 3: KACBCDAD
以1串中的子串与2,3串匹配。分解1串成子串:
len=8: ABCDAFKD
len=7: ABCDAFK
BCDAFKD
len=6: ABCDAF
BCDAFK
CDAFKD
len=5: ABCDA
BCDAF
CDAFK
DAFKD
len=4: ABCD
BCDA
CDAF
DAFK
AFKD
len=3: 时己经不用判断了,最长公共串为BCDA。
就按上面的子串一一与2,3串进行匹配。
#include<stdio.h>char str[12][65],ch[65];void panduan(int l,int r,int flog){ int i,j; if(flog==0) { for( i=0;l<=r;l++,i++) ch[i]=str[1][l]; return ; } for( i=0,j=l;j<=r;i++,j++) if(str[1][j]<ch[i]) break; if(j<=r) { for(i=0,j=l;j<=r;j++,i++) ch[i]=str[1][j]; }}int main(){ int cas,m,l,r; scanf("%d",&cas); while(cas--) { scanf("%d",&m); for(int k=1;k<=m;k++) scanf("%s",str[k]); int len,mov,k,t,i,j,flog=0; for(len=60;len>=3;len--)//最长分共长度为len { l=0; r=len-1;//第1个串中最长公共串的最左,右 for(mov=0;mov<=60-len;mov++)//在长度为len下,在串1中有60-len种长度为len的串 { l=mov; r+=mov;//长度为len的串在串1中的位置 for(k=2;k<=m;k++)//与第2~m个串匹配 { for( t=0;t<=60-len;t++)//在第k个串中以t位置为带头进行匹配 { for(i=l,j=t;i<=r;j++,i++) if(str[1][i]!=str[k][j]) break;//在第k串中没找到完全匹配 if(i>r) break;//在第k串中找到匹配的串 } if(t>60-len) break;//在第k个串中没有找到匹配的那么一定不是公共串,那后串就不用找了 } if(k>m){//找到最长公共串 panduan(l,r,flog);//找到最长公共串中字典序最小的 flog=1; } r-=mov;//复原 } if(flog) break;//找到最长公共串 } if(flog) { for(i=0;i<len;i++) printf("%c",ch[i]); printf("\n"); } else printf("no significant commonalities\n"); }}
1 0
- poj3080Blue Jeans(在m个串中找到这m个串的 最长连续公共子序列)
- POJ 3080:Blue Jeans:枚举求解n个字符串的最长公共连续子串
- POJ 3080 Blue Jeans(KMP:最长连续公共子序列)
- 最长公共子序列(仅借助数组dp本身在O(m+n)时间内构造最长公共子序列)
- poj 1458 dp [最长公共子序列 O(n*m)]
- “最长上升子序列,最大连续子序列和,最长公共子串”的Java实现
- 最长连续公共子序列
- 最长连续公共子序列
- 最长公共子序列(连续)
- 最长公共子序列VS最长连续公共子序列
- 动态规划 最长公共子序列LCS、最长公共连续子串、最长重复子串
- POJ3080 Blue Jeans 最长公共子串
- 最长连续公共最长子序列
- 最长公共连续子串和最长连续公共子序列
- 最长递增子序列(nlogn),最长公共子串(连续),最长公共子序列(不连续)
- 最长回文子序列+最长公共子序列+最长连续回文子串
- 时间复杂度为O(m*n)最长公共子串
- 三个或N个串的最长公共子序列
- 一种根据URL参数条件动态生成URL的方法
- Oracle rank和dense_rank排名函数
- MyEclipse 6.5 Blue Edition - Milestone 1版本获取注册码
- JavaScript笔记
- C#通过多线程为基于 .NET 的应用程序实现响应迅速的用户
- poj3080Blue Jeans(在m个串中找到这m个串的 最长连续公共子序列)
- 我治大学生抄作业毛病的办法——兼答丁又专老师
- h264和x264的区别
- DB2与Oracle相关的东东
- SQL遞歸查詢實戰
- java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver cannot be found by TPTLibrarySyste
- 设置ListView的item在press状态时候的颜色
- XStream指定别名
- 设计模式之工厂方法模式