POJ--1699 Best Sequence(DP+dfs)
Best Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5543 Accepted: 2188 Description
The twenty-first century is a biology-technology developing century. One of the most attractive and challenging tasks is on the gene project, especially on gene sorting program. Recently 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). Given several segments of a gene, you are asked to make a shortest sequence from them. The sequence should use all the segments, and you cannot flip any of the segments.
For example, given ‘TCGG’, ‘GCAG’, ‘CCGC’, ‘GATC’ and ‘ATCG’, you can slide the segments in the following way and get a sequence of length 11. It is the shortest sequence (but may be not the only one).
Input
The first line is an integer T (1 <= T <= 20), which shows the number of the cases. Then T test cases follow. The first line of every test case contains an integer N (1 <= N <= 10), which represents the number of segments. The following N lines express N segments, respectively. Assuming that the length of any segment is between 1 and 20. Output
For each test case, print a line containing the length of the shortest sequence that can be made from these segments. Sample Input
1 5 TCGG GCAG CCGC GATC ATCG Sample Output
11 之所以说是DP,是因为这道题目实现把每两个字符串连接的状态保存起来,而后进行dfs遍历,最后选出最优值
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
using namespace std;
char a[11][25];
int dp[11][11];
int n;
int ans;
int vis[15];
void add(int m,int n)
{
int k=0;
int len1=strlen(a[m]);
int len2=strlen(a[n]);
bool tag;
for(int p=1;p<=len1&&p<=len2;p++)
{
tag=true;
for(int i=0,j=len1-p;i<p;i++,j++)
{
if(a[m][j]!=a[n][i])
{
tag=false;
break;
}
}
if(tag)
k=p;
}
dp[m][n]=len2-k;
}
void dfs(int pre,int num,int sum)
{
if(num==n)
{
if(ans>sum)
ans=sum;
return;
}
for(int i=0;i<n;i++)
{
if(vis[i]==0)
{
vis[i]=1;
dfs(i,num+1,sum+dp[pre][i]);
vis[i]=0;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++)
scanf("%s",a[i]);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
add(i,j);
memset(vis,0,sizeof(vis));
ans=9999;
for(int i=0;i<n;i++)
{
vis[i]=1;
dfs(i,1,strlen(a[i]));
vis[i]=0;
}
printf("%dn",ans);
}
return 0;
}
- 人工智能时代家长不必做“虎妈”
- WCF 消息帧格式
- C#以post方式调用struts rest-plugin service的问题
- Web前端开发初级阶段需要学习的知识有哪些?
- 作为TensorFlow的底层语言,你会用C+构建深度神经网络吗?
- 企业应用中使用Silverlight 3
- oracle:db-link使用
- 5个炫酷的Python工具,你都用过么?
- 保护ASP.NET 应用免受 CSRF 攻击
- .NET:Entity Framework 笔记
- redis 学习笔记(1)-编译、启动、停止
- 致研究者:2018 AI研究趋势
- redis 学习笔记(3)-master/slave(主/从模式)
- 解决Chrome或其它WebKit浏览器input和textarea的黄色/蓝色边框问题
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- yum安装与配置 Tomcat
- org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, “xxxx“)] with root cause
- UnsatisfiedDependencyException
- Java8 Collectors.toMap的key重复
- bean无法注入(与文件包位置有关)及修改包项目、model名
- Jackson第一篇
- jackson第二篇
- 从源码分析常见集合的区别之List接口
- c++ 调用ffmpeg命令获取视频属性
- Kubernetes 1.19.0——网络策略
- TypeScript 参数简化实战(进阶知识点conditional types,中高级必会)
- 最简实现Promise,支持异步链式调用(20行)
- 40行代码把Vue3的响应式集成进React做状态管理
- 写给女朋友的中级前端面试秘籍(含详细答案,15k级别)
- 写给初中级前端的高级进阶指南