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)的更多相关文章

  1. 洛谷 P1164:小A点菜(DP/DFS)

    题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家--餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:"随便点". 题目描述 不过ui ...

  2. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  3. POJ 1015 Jury Compromise(dp坑)

    提议:在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑选n个人作为陪审团的候选人,然后再从这n个人中选m人组成陪审团.选m人的办法是:控方和辩方会根据对候选 ...

  4. POJ 1699 Best Sequence(DFS)

    題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做 ...

  5. [luoguP1433] 吃奶酪(DP || Dfs)

    传送门 深搜加剪纸可A(O(玄学) 1274ms) ——代码 #include <cmath> #include <cstdio> #include <iostream& ...

  6. POJ 2711 Regular Words(DP + 高精度)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1711 题目大意:给定一个正整数n,产生一个3*n位长的串,要求这个串 ...

  7. CF13C Sequence(DP+离散化)

    题目描述 给定一个序列,每次操作可以把某个数+1-1.要求把序列变成非降数列.求最少的修改次数. 输入输出样例 输入 #1 - 输出 #1 4 输入 #2 输出 #2 1 解题思路 这题是一道非常好题 ...

  8. POJ 3581:Sequence(后缀数组)

    题目链接 题意 给出n个数字的序列,现在让你分成三段,使得每一段翻转之后拼接起来的序列字典序最小.保证第一个数是序列中最大的数. 例如样例是{10, 1, 2, 3, 4},分成{1, 10}, {2 ...

  9. poj 3176 Cow Bowling(dp基础)

    Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...

随机推荐

  1. Linux 查看磁盘分区、文件系统、磁盘的使用情况相关的命令和工具介绍

    https://www.cnblogs.com/alexyuyu/articles/3454907.html

  2. python爬虫系列:做一个简单的动态代理池

    自动 1.设置动态的user agent import urllib.request as ure import urllib.parse as upa import random from bs4 ...

  3. Python easyGUI 文件浏览 显示文件内容

    #提供一个文件浏览夹.让用户选择需要打开的文件,打开并显示文件内容: import easygui as g import os msg='浏览文件并打开' title='测试' default='D ...

  4. TTL值

    我们在解析域名时经常出现 TTL 这个字段,里面默认写的是 10 分钟. 另外,有时候我们 ping 某域名或 IP 的时候,会出现 TTL= XXX. 一.什么是域名的 TTL 值? TTL(Tim ...

  5. 【代码审计】iZhanCMS_v2.1 前台存储型XSS漏洞分析

      0x00 环境准备 iZhanCMS官网:http://www.izhancms.com 网站源码版本:爱站CMS(zend6.0) V2.1 程序源码下载:http://www.izhancms ...

  6. 【RF库Collections测试】Reverse List

    Name:Reverse ListSource:Collections <test library>Arguments:[ list_ ]Reverses the given list i ...

  7. Install VMware Workstation as a Service

    Under default conditions, VMware Workstation does not support the ability to run virtual machines as ...

  8. Splash 对象属性

    args js_enabled resource_timeout images_enabled plugins_enabled scroll_position

  9. mybatis 之 parameterType="List" 2

    <select id="queryGoodsByGoodsNoPcweb" parameterType="List" resultMap="si ...

  10. React Native(十四)——Slider

    最近我们rn版的App快要内测了,小伙伴们都在积极的改bug,于是在其中就遇到了关于Slider的部分小知识,特地记录自己用到的部分属性,也许恰好会帮助到用到该组件的你: 属性罗列(https://r ...