POJ 1795
2014-03-09 13:32
399 查看
DNA Laboratory
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
Sample Output
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
View Code
Time Limit: 5000MS | Memory Limit: 30000K | |
Total Submissions: 1425 | Accepted: 280 |
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[20][maxn]; int dp[20][(1 << 15) + 5],dis[20][20]; bool vis[20],done[20]; string ans; int cal(int x,int y) { int _max = 0; for(int i = 0; i < strlen(s[x]); i++) { if(s[x][i] != s[y][0]) continue; int j,k; for( j = i,k = 0; 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] = 1; break; } if(j == strlen(s[x])) { _max = max(_max,j - i); } } return -_max; } void init() { for(int u = 0; u < n; u++) { if(done[u]) continue; for(int v = 0; v < n; v++) { if(u == v || done[v]) continue; dis[u][v] = cal(u,v); } } } void dfs(int v,int s1) { vis[v] = 1; int id = -1; string t("z"); for(int u = 0; u < n; u++) { if(done[u] || vis[u]) continue; if(dp[v][s1] == dp[u][s1 | (1 << 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 != -1) { ans = ans + t; dfs(id,s1 | (1 << id)); } } void solve() { init(); for(int s1 = (1 << n) - 2; s1; s1--) { for(int v = 0; v < n; v++) { if(!(s1 & (1 << v)) || done[v]) continue; for(int u = 0; u < n; u++) { if(u == v || (s1 & (1 << u)) || done[v] ) continue; dp[v][s1] = min(dp[v][s1],dp[u][s1 | (1 << u)] + dis[v][u]); } } } int _min = 0; for(int i = 0; i < n; i++) { if(done[i]) continue; _min = min(_min,dp[i][1 << i]); } memset(vis,0,sizeof(vis)); ans = "z"; int id; for(int i = 0; i < n; i++) { if(done[i]) continue; string t(s[i]); if(dp[i][1 << i] == _min && ans > t) { ans = t; id = i; } } dfs(id,1 << id); printf("Scenario #%d:\n",ca++); cout << ans << endl; } int main() { int t; //freopen("sw.in","r",stdin); scanf("%d",&t); ca = 1; while(t--) { memset(done,0,sizeof(done)); scanf("%d",&n); for(int i = 0; i < n; i++) { scanf("%s",s[i]); } memset(dis,0,sizeof(dis)); for(int i = 0; i < n; i++) { for(int s = 0; s < (1 << n); s++) { dp[i][s] = 0; } } solve(); printf("\n"); } return 0; }
View Code
相关文章推荐
- ASP.NET的控件
- c++类中的静态成员和非静态成员
- ASP.NET的错误集
- 32位到64位:VS2013修改编译平台的方法
- Hyper-V升级系列PART3:2008R2SP1 Hyper-V群集升级至2012
- APP常用字体
- 【C++】PAT(advanced level)1044. Shopping in Mars (25)
- iOS 网络/本地 图片 按自定义比例缩放 不失真 方法
- Eclipse快捷键组合
- Linux中使用unzip命令解压缩zip文件到特定目录
- RocksDB在cygwin编译与使用
- JSP include编译指令和include动作指令的区别
- ASP.Net的CSS
- JSON详解
- Epsilon闭包计算
- 成绩统计
- 枚举类型求点的对称坐标
- 信息化建设适应当前改革的思考——简化流程与信息透明
- Ajax中get和post请求的区别
- Java错误提示is not an enclosing class