POJ 1795 DNA Laboratory (贪心+状压DP)
Posted dwtfukgv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1795 DNA Laboratory (贪心+状压DP)相关的知识,希望对你有一定的参考价值。
题意:给定 n 个 字符串,让你构造出一个最短,字典序最小的字符串,包括这 n 个字符串。
析:首先使用状压DP,是很容易看出来的,dp[s][i] 表示已经满足 s 集合的字符串以 第 i 个字符串结尾,他很容易就求得最短长度,但是这个字符串怎么构造呢,
由于要字典序最小,所以就不好搞了,挺麻烦的,所以我们利用贪心的思路,我们可以这样定义,dp[s][i] 表示已经满足 s 集合的字符串以 第 i 个字符串开头,
从后向前放,状态转移方程为:dp[s|(1<<i)][i] = min{ dp[s][j] + dist[k][j] },dist[k][j] 表示把 k 放到 j 前面所要的最短长度,这个数组我们可以通过预处理来得到,
注意这里是把 k 放到 j 的前面,不是把 j 放到 k 的后面。最后还要注意把包含的字符串去掉,还有如果全为一样的情况,要注意特殊判断,这个点我RE了一晚上。。。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e16; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 1e5 + 10; const int mod = 100000000; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int dp[1<<16][16], dist[20][20]; string str[20], ans; vector<string> v; void init(){ for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) if(i != j){ int t = min(v[i].size(), v[j].size()); dist[i][j] = 0; for(int k = t; k >= 0; --k) if(v[i].substr(v[i].size() - k) == v[j].substr(0, k)){ dist[i][j] = v[i].size() - k; break; } } } void dfs(int id, int s){ if(s == 0) return ; string ss = "Z"; int x; for(int i = 0; i < n; ++i){ if(!(s&(1<<i))) continue; if(dp[s|(1<<id)][id] == dp[s][i] + dist[id][i]){ int xx = v[id].size() - dist[id][i]; string sss = v[i].substr(xx); if(ss > sss) ss = sss, x = i; } } ans += ss; dfs(x, s^(1<<x)); } int main(){ ios_base::sync_with_stdio(false); int T; cin >> T; for(int kase = 1; kase <= T; ++kase){ cin >> n; cout << "Scenario #" << kase << ":" << endl; for(int i = 0; i < n; ++i) cin >> str[i]; v.clear(); for(int i = 0; i < n; ++i){ bool ok = true; for(int j = 0; j < n; ++j){ if(i == j || str[i].size() > str[j].size()) continue; if(str[j].find(str[i]) != string::npos){ ok = false; break; } } if(ok) v.push_back(str[i]); } if(v.empty()){ cout << str[0] << endl << endl; continue; } sort(v.begin(), v.end()); n = v.size(); init(); int all = 1<<n; memset(dp, INF, sizeof dp); for(int i = 0; i < n; ++i) dp[1<<i][i] = v[i].size(); for(int i = 0; i < all; ++i) for(int j = 0; j < n; ++j){ if(dp[i][j] == INF) continue; for(int k = 0; k < n; ++k){ if(i & (1<<k)) continue; dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + dist[k][j]); } } int id = 0; for(int i = 0; i < n; ++i) if(dp[all-1][id] > dp[all-1][i]) id = i; ans = v[id]; dfs(id, (all-1)^(1<<id)); cout << ans << endl << endl; } return 0; }
以上是关于POJ 1795 DNA Laboratory (贪心+状压DP)的主要内容,如果未能解决你的问题,请参考以下文章