DNA Laboratory
Time Limit: 5000MS   Memory Limit: 30000K
Total Submissions: 1425   Accepted: 280

Description

Background
Having started to build his own DNA lab just recently, the evil
doctor Frankenstein is not quite up to date yet. He wants to extract his
DNA, enhance it somewhat and clone himself. He has already figured out
how to extract DNA from some of his blood cells, but unfortunately
reading off the DNA sequence means breaking the DNA into a number of
short pieces and analyzing those first. Frankenstein has not quite
understood how to put the pieces together to recover the original
sequence.

His pragmatic approach to the problem is to sneak into university
and to kidnap a number of smart looking students. Not surprisingly, you
are one of them, so you would better come up with a solution pretty
fast.

Problem

You are given a list of strings over the alphabet A (for adenine), C
(cytosine), G (guanine), and T (thymine),and your task is to find the
shortest string (which is typically not listed) that contains all given
strings as substrings.

If there are several such strings of shortest length, find the smallest in alphabetical/lexicographical order.

Input

The first line contains the number of scenarios.

For each scenario, the first line contains the number n of strings
with 1 <= n <= 15. Then these strings with 1 <= length <=
100 follow, one on each line, and they consist of the letters "A", "C",
"G", and "T" only.

Output

The
output for every scenario begins with a line containing "Scenario #i:",
where i is the number of the scenario starting at 1. Then print a
single line containing the shortest (and smallest) string as described
above. Terminate the output for the scenario with a blank line.

Sample Input

1
2
TGCACA
CAT

Sample Output

Scenario #1:
TGCACAT

Source

TUD Programming Contest 2004, Darmstadt, Germany
 
 
好恶心的状态dp啊!!!!
首先对消除能被其他串包含的串,然后对剩下的串进行建边,建一个有向图,长度是位于一个边指向的反方向所对应的串的后缀与边指向的串的相同的最大前缀长度的负数。
设dp[v][s] 是以v为终点,已经访问过集合s所对应的点的最小值,可建立如下方程
dp[v][s] = min(dp[u ][s | (1 << u)] + dis[v][u])       !((1 << u) & s) = = 1
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define maxn 105 #define INF 10000 int n,ca,len,sum;
char s[][maxn];
int dp[][( << ) + ],dis[][];
bool vis[],done[];
string ans; int cal(int x,int y) {
int _max = ;
for(int i = ; i < strlen(s[x]); i++) {
if(s[x][i] != s[y][]) continue;
int j,k;
for( j = i,k = ; j < strlen(s[x]) && k < strlen(s[y]); j++,k++) {
if(s[x][j] != s[y][k]) break;
}
if(k == strlen(s[y])) {
done[y] = ;
break;
}
if(j == strlen(s[x])) {
_max = max(_max,j - i); }
} return -_max;
}
void init() {
for(int u = ; u < n; u++) {
if(done[u]) continue;
for(int v = ; v < n; v++) {
if(u == v || done[v]) continue;
dis[u][v] = cal(u,v); }
} } void dfs(int v,int s1) {
vis[v] = ;
int id = -;
string t("z");
for(int u = ; u < n; u++) {
if(done[u] || vis[u]) continue; if(dp[v][s1] == dp[u][s1 | ( << u)] + dis[v][u]) {
string t1(s[u] - dis[v][u],s[u] + strlen(s[u]));
if(t > t1) {
t = t1;
id = u;
}
} } if(id != -) {
ans = ans + t;
dfs(id,s1 | ( << id)); }
} void solve() {
init(); for(int s1 = ( << n) - ; s1; s1--) {
for(int v = ; v < n; v++) {
if(!(s1 & ( << v)) || done[v]) continue;
for(int u = ; u < n; u++) {
if(u == v || (s1 & ( << u)) || done[v] ) continue;
dp[v][s1] = min(dp[v][s1],dp[u][s1 | ( << u)] + dis[v][u]); }
}
} int _min = ;
for(int i = ; i < n; i++) {
if(done[i]) continue;
_min = min(_min,dp[i][ << i]);
} memset(vis,,sizeof(vis)); ans = "z";
int id;
for(int i = ; i < n; i++) {
if(done[i]) continue;
string t(s[i]);
if(dp[i][ << i] == _min && ans > t) {
ans = t;
id = i;
}
} dfs(id, << id); printf("Scenario #%d:\n",ca++);
cout << ans << endl; }
int main()
{
int t;
//freopen("sw.in","r",stdin);
scanf("%d",&t);
ca = ; while(t--) {
memset(done,,sizeof(done)); scanf("%d",&n); for(int i = ; i < n; i++) {
scanf("%s",s[i]);
} memset(dis,,sizeof(dis)); for(int i = ; i < n; i++) {
for(int s = ; s < ( << n); s++) {
dp[i][s] = ;
}
} solve();
printf("\n"); } return ;
}

POJ 1795的更多相关文章

  1. POJ 1795 DNA Laboratory(状压DP)

    [题目链接] http://poj.org/problem?id=1795 [题目大意] 给出n个字符串,求一个最小长度的串,该串包含给出的所有字符串. 要求长度最小且字典序最小. [题解] dp[i ...

  2. POJ 1795 DNA Laboratory (贪心+状压DP)

    题意:给定 n 个 字符串,让你构造出一个最短,字典序最小的字符串,包括这 n 个字符串. 析:首先使用状压DP,是很容易看出来的,dp[s][i] 表示已经满足 s 集合的字符串以 第 i 个字符串 ...

  3. poj 1795 DNA Laboratory

    DNA Laboratory Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 2892   Accepted: 516 Des ...

  4. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  5. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  6. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  7. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  8. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  9. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

随机推荐

  1. mongodb gdal 矢量数据格式驱动

    写了个mongodb的gdal driver,放在了github上,如果你需要,欢迎加入mongogis group. 直接的效果是使得QGIS, GeoServer, MapServer, ArcG ...

  2. jQuery checkBox 全选的例子

    表单处理时经常会有全选的功能,但是这个功能往往会被忽视一个细节,就是逐个选中 checkBox 直至全选时,经常会忘记修改全选 checkBox 的状态,某知名互联网公司的网盘就会出现这样的问题,问题 ...

  3. Linux设备驱动01

    准备: 1.英语-是工具 2.电路原理图-补充“数电”的知识 3.阅读内核代码的能力 linux:vi+ctags+cscope windows:source insight 驱动开发的步骤 I.编辑 ...

  4. 随便写了一个DAO

    package com.java; public class ExamStudent { /** * 流水号 */ private int flowId; /** * 四级.六级 */ private ...

  5. java中对集合对象list的几种循环访问

    java中对集合对象list的几种循环访问的总结如下 1 经典的for循环 public static void main(String[] args) { List<String> li ...

  6. 【Sharing】如何成为一名黑客

    [声明]此文为转载,只为收藏. 从小到大听说了无数关于“电脑黑客”的故事,比如XXX入侵美国五角大楼,再比如前几年的“熊猫烧香”病毒,这些故事的主角都被我们的媒体称之为“黑客”.其实这些人,更大程度上 ...

  7. 局域网实现 yum

    1 安装squid代理 ##### . 安装squid yum -y remove squid yum -y install squid ##### . 修改配置文件 vi /etc/squid/sq ...

  8. php进行多个数组合并zip

    $a = array_zip(array("Dog","Cat","Horse"), array(1,2,3), array("l ...

  9. JavaScript中使用console调试程序的坑

    上DEMO a = {key1: [1, 2], 'key2': {'key4': '11'}, 'key3': [1, 2]} console.info(1,a) a.key2.key4 = '22 ...

  10. parameter和argument的区别

    根据网上一些资料,对parameter和argument的区别,做如下的简单说明.1. parameter是指函数定义中参数,而argument指的是函数调用时的实际参数.2. 简略描述为:param ...