DNA sequence

Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

The twenty-first century is a biology-technology developing century. We know that a gene is made of DNA. The nucleotide bases from which DNA is built are A(adenine), C(cytosine), G(guanine), and T(thymine). Finding the longest common subsequence between DNA/Protein
sequences is one of the basic problems in modern computational molecular biology. But this problem is a little different. Given several DNA sequences, you are asked to make a shortest sequence from them so that each of the given sequence is the subsequence
of it.



For example, given "ACGT","ATGC","CGTT" and "CAGT", you can make a sequence in the following way. It is the shortest but may be not the only one.



Input

The first line is the test case number t. Then t test cases follow. In each case, the first line is an integer n ( 1<=n<=8 ) represents number of the DNA sequences. The following k lines contain the k sequences, one per line. Assuming that the length of any
sequence is between 1 and 5.

Output

For each test case, print a line containing the length of the shortest sequence that can be made from these sequences.

Sample Input

1
4
ACGT
ATGC
CGTT
CAGT

Sample Output

8

————————————————————————————————————————————————
题意:从n个串中找出一个最短的公共串(也许应该说序列吧,因为不要求连续,即只要保持相对顺序就好)。



分析:迭代加深搜索,就是每次都限制了DFS的深度,若搜不到答案,则加深深度,继续搜索,这样就防止了随着深度不断加深而进行的盲目搜索,而且,对于这种求最短长度之类的题目,只要找到可行解,即是最优解了。所以就这样敲完代码了,敲完之后,悲剧TLE。



少了一步十分重要的剪枝,就是每次DFS的时候,都要判断一下,当前的深度+最少还有加深的深度是否大于限制的长度,若是,则退回。



#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
char s[10][10];
int n,mx,flag;
char ch[5]={'A','T','C','G'}; void dfs(int deep,int *cnt)
{
if(flag)
return;
int sum=0;
int maxlen=0;
int a;
for(int i=0; i<n; i++)
{
a=strlen(s[i])-cnt[i];
sum=sum+a;
maxlen=max(maxlen,a);
}
if(maxlen+deep>mx)
return;
if(sum==0)
{
flag=1;
return;
}
int fl=0;
int next[10];
for(int i=0; i<4; i++)
{
for(int j=0; j<n; j++)
{
if(s[j][cnt[j]]==ch[i])
{
fl=1;
next[j]=cnt[j]+1;
}
else
next[j]=cnt[j];
}
if(fl)
{
dfs(deep+1,next);
}
if(flag)
return;
} } int main()
{
int o,k;
int cnt[10];
scanf("%d",&o);
while(o--)
{
scanf("%d",&n);
mx=0;
for(int i=0; i<n; i++)
{
scanf("%s",s[i]);
k=strlen(s[i]);
mx=max(mx,k);
}
memset(cnt,0,sizeof(cnt));
flag=0;
for(int i=0; i<40; i++)
{
dfs(0,cnt); if(flag)
break;
mx++;
}
printf("%d\n",mx);
}
return 0;
}

Hdu1560 DNA sequence(IDA*) 2017-01-20 18:53 50人阅读 评论(0) 收藏的更多相关文章

  1. NYOJ-102 次方求模 AC 分类: NYOJ 2014-02-06 18:53 184人阅读 评论(0) 收藏

    地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=102 //a^b mod c=(a mod c)^b mod c很容易设计出一个基于二分的递归 ...

  2. ZEDBOARD启动自启配置(加载镜像) 分类: OpenCV ubuntu shell ZedBoard Eye_Detection 2014-11-08 18:53 167人阅读 评论(0) 收藏

    参考:陆书14.2.8 1)备份ramdisk8M.image.gz 2)加载rootfs镜像文件: 3)在镜像目录下建立自己所需文件夹(挂载目录): 我需要的挂载目录有两个: root/qt/ins ...

  3. c++map的用法 分类: POJ 2015-06-19 18:36 11人阅读 评论(0) 收藏

    c++map的用法 分类: 资料 2012-11-14 21:26 10573人阅读 评论(0) 收藏 举报 最全的c++map的用法 此文是复制来的0.0 1. map最基本的构造函数: map&l ...

  4. 团体程序设计天梯赛L2-003 月饼 2017-03-22 18:17 42人阅读 评论(0) 收藏

    L2-003. 月饼 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不 ...

  5. HDU6205 Coprime Sequence 2017-05-07 18:56 36人阅读 评论(0) 收藏

    Coprime Sequence                                                        Time Limit: 2000/1000 MS (Ja ...

  6. 移植QT到ZedBoard(制作运行库镜像) 交叉编译 分类: ubuntu shell ZedBoard OpenCV 2014-11-08 18:49 219人阅读 评论(0) 收藏

    制作运行库 由于ubuntu的Qt运行库在/usr/local/Trolltech/Qt-4.7.3/下,由makefile可以看到引用运行库是 INCPATH = -I/usr//mkspecs/d ...

  7. highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏

    /*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...

  8. HDU6023 Automatic Judge 2017-05-07 18:30 73人阅读 评论(0) 收藏

    Automatic Judge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. Pots 分类: 搜索 POJ 2015-08-09 18:38 3人阅读 评论(0) 收藏

    Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11885 Accepted: 5025 Special Judge D ...

随机推荐

  1. Spark系列(二) Spark Shell各种操作及详细说明

    并行化scala集合(Parallelize) //加载数据1~10 val num=sc.parallelize(1 to 10) //每个数据项乘以2,注意 _*2记为一个函数(fun) val ...

  2. 基于Redis实现简单的分布式锁

      在分布式场景下,有很多种情况都需要实现最终一致性.在设计远程上下文的领域事件的时候,为了保证最终一致性,在通过领域事件进行通讯的方式中,可以共享存储(领域模型和消息的持久化数据源),或者做全局XA ...

  3. adb端口占用及模拟器调试

    首先在使用ADB前所有手机辅助类软件 1.CMD命令窗口输入:adb nodaemon server .然后就会提示你哪个端口被占用了. 2.输入netstat -ano | findstr &quo ...

  4. ORA-00257:archiver error.Connect internal only, until freed的问题(转)

    删除归档日志_ORA-00257:archiver error.Connect internal only, until freed的问题   ORA-00257: archiver error. C ...

  5. 为Web页中的Table对象创建一个映射表

    HTML对象中的TABLE是我们常用的网页元素,在DHTML编程中,我们可以通过它的rows和cells方法方便的访问表格对象里面的每一个单元格,而且表格对象(table)的每个单元行(tr)和每个单 ...

  6. centos7.3给squid搭建代理服务器添加认证nginx

    1先安装 nginx 这里是教程 点击查看 2 然后 使用命令 创建用户 htpasswd -c /etc/nginx/passwd.db baker 输入密码  提示添加完毕 3 查看加密后的用户和 ...

  7. 关于phpmailer邮件发送

    今天有个需求,要把phpmailer集成到框架里面 所以我去官方下载了 phpmail5.2.6 地址在 https://github.com/PHPMailer/PHPMailer/releases ...

  8. Rhythmk 一步一步学 JAVA (21) JAVA 多线程

    1.JAVA多线程简单示例 1.1 .Thread  集成接口 Runnable 1.2 .线程状态,可以通过  Thread.getState()获取线程状态: New (新创建) Runnable ...

  9. leetcode921

    public class Solution { public int MinAddToMakeValid(string S) { Stack<char> ST = new Stack< ...

  10. Eclipse使用xdoclet1.2.3 生成hibernate配置文件和映射文件

    用ant和xdoclet生成hibernate配置文件可以为我们省去很多配置的操作,废话不多说,直接给栗子: 测试环境: eclipse:Eclipse Java EE IDE for Web Dev ...