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. eclipse 搜索 正则表达式

    1.换行搜索,如下: \.dyform\([\r]*[\s]*\{

  2. Go 语言机制之逃逸分析

    https://blog.csdn.net/weixin_38975685/article/details/79788254   Go 语言机制之逃逸分析 https://blog.csdn.net/ ...

  3. python登录网易163邮箱,爬取邮件

    from common import MyRequests,LoggerUntil,handle_exception myRequests.update_headers({ 'Accept':'tex ...

  4. 如何打造千万级Feed流系统

    from:https://www.cnblogs.com/taozi32/p/9711413.html 在互联网领域,尤其现在的移动互联网时代,Feed流产品是非常常见的,比如我们每天都会用到的朋友圈 ...

  5. Oracle之表空间基于时间点的恢复

    记一次优化过程中:一次误操作,在不影响其他表空间的情况下:采用表空间基于时间点的恢复(TSPITR)方法恢复数据的过程. 1.TSPITR恢复原理    TSPITR目前最方便的方法是使用RMAN进行 ...

  6. 【代码审计】QYKCMS_v4.3.2 任意文件读取漏洞分析

      0x00 环境准备 QYKCMS官网:http://www.qykcms.com/ 网站源码版本:QYKCMS_v4.3.2(企业站主题) 程序源码下载:http://bbs.qingyunke. ...

  7. WopiServerTutorial

    Program.cs using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...

  8. Explaining Delegates in C# - Part 2 (Events 1)

    In my previous post, I spoke about a few very basic and simple reasons of using delegates - primaril ...

  9. mybatis 之resultType="HashMap" parameterType="list"

    <!-- 查询商品仓库信息 --> <select id="loadGoodsStock" resultType="HashMap" para ...

  10. Android的Fragment中onActivityResult不被调用

    1.检查该Fragment所属的Activity中,是否重写了onActivityResult方法. 2.检查Fragment中的startActivityForResult的调用方式. 请确保不要使 ...