POJ 1795
Time Limit: 5000MS | Memory Limit: 30000K | |
Total Submissions: 1425 | Accepted: 280 |
Description
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
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
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
#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的更多相关文章
- POJ 1795 DNA Laboratory(状压DP)
[题目链接] http://poj.org/problem?id=1795 [题目大意] 给出n个字符串,求一个最小长度的串,该串包含给出的所有字符串. 要求长度最小且字典序最小. [题解] dp[i ...
- POJ 1795 DNA Laboratory (贪心+状压DP)
题意:给定 n 个 字符串,让你构造出一个最短,字典序最小的字符串,包括这 n 个字符串. 析:首先使用状压DP,是很容易看出来的,dp[s][i] 表示已经满足 s 集合的字符串以 第 i 个字符串 ...
- poj 1795 DNA Laboratory
DNA Laboratory Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 2892 Accepted: 516 Des ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
随机推荐
- 通过Driver获取数据库连接
先看一下文件,在当前包下有一个properties配置文件,在根目录下有一个lib文件夹,里面放的是mySql的驱动jar包 Driver :是一个接口,数据库厂商必须提供实现的接口,能从其中获取数据 ...
- Linux命令行提示符设置
我们使用Linux系统时接触最多的是它的命令行窗口,很多时候我们都需要在命令行上输入命令,在输入的命令前都会有提示符,一般系统默认的提示符形式是:[username@host 工作目录]$. 其实,我 ...
- 《DDNS服务器的搭建和案例解决方法》
DDNS原理:DNS + DHCP =DDNS DHCP负责ip解析,和分配给客户机ip,ip为随机数. DNS负责域名解析,A记录里记录了每个ip对应的域名. 客户端ip肯定是变化的,不可能一直使用 ...
- webpack使用webpack-dev-middleware进行热重载
新手,刚开始学习webpack,想使用webdevserver,但定制性太差,于是研究了一下使用webpack-dev-middleware进行指定. 根据文档https://www.npmjs.co ...
- 方法的可变长参数 传入参数个数不确定可用(Type ... values)
/** * 可变长的参数. * 有时候,我们传入到方法的参数的个数是不固定的,为了解决这个问题,我们一般采用下面的方法: * 1. 重载,多重载几个方法,尽可能的满足参数的个数.显然这不是什么好办法. ...
- [Android教程]TextView使用SpannableString设置复合文本
TextView通常用来显示普通文本,但是有时候需要对其中某些文本进行样式.事件方面的设置.Android系统通过SpannableString类来对指定文本进行相关处理,具体有以下功能: 1.Bac ...
- apk反编译之二——smali学习
在apk被反编译后,原来的java程序会以smali文件呈现.这就需要补充smali的知识.依旧参考官方文档,择日我将把官方文档做一下翻译.今日先贴出链接地址: 1:了解smali字节码的寄存器 请参 ...
- mdelay,udelay,msleep区别
delay函数是忙则等待,占用CPU时间:而sleep函数使调用的进程进行休眠. udelay引用头文件/include/asm-***/delay.h,mdelay和ndelay则引用/includ ...
- linux编码
转: Linux查看文件编码格式及文件编码转换 如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而L ...
- 【Node.app】Node.js for iOS
Node.app 是用于 iOS 开发的 Node.js 解释器,它允许最大的代码重用和快速创新,占用资源很少,为您的移动应用程序提供 Node.js 兼容的 JavaScript API.你的客户甚 ...