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("%d\n",ans);
}
return 0;
}
POJ--1699 Best Sequence(DP+dfs)的更多相关文章
- 洛谷 P1164:小A点菜(DP/DFS)
题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家--餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:"随便点". 题目描述 不过ui ...
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- POJ 1015 Jury Compromise(dp坑)
提议:在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑选n个人作为陪审团的候选人,然后再从这n个人中选m人组成陪审团.选m人的办法是:控方和辩方会根据对候选 ...
- POJ 1699 Best Sequence(DFS)
題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做 ...
- [luoguP1433] 吃奶酪(DP || Dfs)
传送门 深搜加剪纸可A(O(玄学) 1274ms) ——代码 #include <cmath> #include <cstdio> #include <iostream& ...
- POJ 2711 Regular Words(DP + 高精度)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1711 题目大意:给定一个正整数n,产生一个3*n位长的串,要求这个串 ...
- CF13C Sequence(DP+离散化)
题目描述 给定一个序列,每次操作可以把某个数+1-1.要求把序列变成非降数列.求最少的修改次数. 输入输出样例 输入 #1 - 输出 #1 4 输入 #2 输出 #2 1 解题思路 这题是一道非常好题 ...
- POJ 3581:Sequence(后缀数组)
题目链接 题意 给出n个数字的序列,现在让你分成三段,使得每一段翻转之后拼接起来的序列字典序最小.保证第一个数是序列中最大的数. 例如样例是{10, 1, 2, 3, 4},分成{1, 10}, {2 ...
- poj 3176 Cow Bowling(dp基础)
Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...
随机推荐
- python登录网易163邮箱,爬取邮件
from common import MyRequests,LoggerUntil,handle_exception myRequests.update_headers({ 'Accept':'tex ...
- Oauth2.0(一):为什么需要 Oauth2.0 协议?
假设有两家互联网企业 A 和 B,其中 B 是一家提供相片云存储的公司.即 B 的用户可以把相片上传到 B 网站上长期保存,然后可以在不同的设备上查看.某一天,A 和 B 谈成了一项合作:希望 B 用 ...
- 如何分析解决Android ANR(转载)
转载自:http://blog.csdn.net/dadoneo/article/details/8270107 一:什么是ANR ANR:Application Not Responding,即应用 ...
- R语言概述
R是一个有着统计分析功能及强大作图功能的软件系统,是由Ross Ihaka和Robert Gentleman共同创立.它是属于GNU系统的一个自由.免费.源码开放的软件,同一时候也是一个用于统计计算和 ...
- Echarts调整图表上下左右的间距,Echarts调整柱状图左右的间距
Echarts调整图表上下左右的间距,Echarts调整柱状图左右的间距 >>>>>>>>>>>>>>>> ...
- PHP代码审计笔记--弱类型存在的安全问题
0x01 前言 PHP 是一门弱类型语言,不必向 PHP 声明该变量的数据类型,PHP 会根据变量的值,自动把变量转换为正确的数据类型. 弱类型比较,是一个比较蛋疼的问题,如左侧为字符串,右侧为一个整 ...
- Kubernetes 简介
一.Kubernetes 相关概念 1. Kubernetes 是一个开源的容器集群管理系统,主要用来自动化部署容器 .自动扩展与收缩容器规模 .提供容器间的负载均衡2. Node:Node(节点)也 ...
- 使用pyenv管理不同的python版本
1. pvenv的安装 git clone https://github.com/yyuu/pyenv.git ~/.pyenv echo 'export PYENV_ROOT="$HOME ...
- Git服务器的搭建和使用
一.GIT服务器的搭建 1. 安装Git yum -y install git 2. 创建git用户 adduser git 3. 创建证书登陆 收集所有客户端需要登录的用户的公钥,就是他们自己的 ...
- [SublimeText] Sublime Text 2 在 Ubuntu 上安装指南
1. 下载Sublime Text 2 在官网下载对应系统位数的版本,从压缩包中提取出源代码,解压后文件夹中的"sublime_text"双击即可直接运行. 2. 建立快捷链接 将 ...