题意:给定 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)的更多相关文章

  1. POJ 1795 DNA Laboratory(状压DP)

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

  2. poj 1795 DNA Laboratory

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

  3. POJ 1185 炮兵阵地(状压DP)

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26426   Accepted: 10185 Descriptio ...

  4. POJ 2411 Mondriaan's Dream -- 状压DP

    题目:Mondriaan's Dream 链接:http://poj.org/problem?id=2411 题意:用 1*2 的瓷砖去填 n*m 的地板,问有多少种填法. 思路: 很久很久以前便做过 ...

  5. POJ 2411 Mondriaan's Dream ——状压DP 插头DP

    [题目分析] 用1*2的牌铺满n*m的格子. 刚开始用到动规想写一个n*m*2^m,写了半天才知道会有重复的情况. So Sad. 然后想到数据范围这么小,爆搜好了.于是把每一种状态对应的转移都搜了出 ...

  6. poj 2288 Islands and Bridges ——状压DP

    题目:http://poj.org/problem?id=2288 状压挺明显的: 一开始写了(记忆化)搜索,但一直T: #include<iostream> #include<cs ...

  7. bzoj3717 [PA2014]Pakowanie 贪心+状压DP

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3717 题解 这道题大概也就只能算常规的状压 DP 吧,但是这个状态和转移的设计还是不是很好想. ...

  8. 【POJ 2923】Relocation(状压DP+DP)

    题意是给你n个物品,每次两辆车运,容量分别是c1,c2,求最少运送次数.好像不是很好想,我看了网上的题解才做出来.先用状压DP计算i状态下,第一辆可以运送的重量,用该状态的重量总和-第一辆可以运送的, ...

  9. POJ 1185 炮兵阵地 (状压DP)

    题目链接 题意 : 中文题不详述. 思路 :状压DP,1表示该位置放炮弹,0表示不放.dp[i][j][k],代表第 i 行的状态为k时第i-1行的状态为 j 时放置的最大炮弹数.只是注意判断的时候不 ...

随机推荐

  1. RabbitMQ和Kafka可靠性

    RabbitMQ和Kafka可靠性 https://www.cnblogs.com/haolujun/p/9641840.html 我们通过前文知道,RabbitMQ的队列分为master queue ...

  2. volatile关键字及内存可见性

    先看一段代码: package com.java.juc; public class TestVolatile { public static void main(String[] args) { T ...

  3. 2017/2/22怎么判断mongodb服务已经启动了?

    打开任务管理器,看看服务下面是否有个MongoDB,有就表示成功

  4. kali_linux学习笔记

    kali linux ssh登陆: 一.配置SSH参数 修改sshd_config文件,命令为: vi /etc/ssh/sshd_config 将#PasswordAuthentication no ...

  5. 在Mac系统下如何恢复SourceTree全局忽略的文件

    在家目录“~”下编辑 “.gitignore_global ” 文件即可:vim  .gitignore_global

  6. Java创建AD(Active Directory)域控制器用户 (未测)

    import java.util.Hashtable; import javax.naming.ldap.*; import javax.naming.directory.*; import java ...

  7. rem怎么计算

    px:相对长度单位.像素px是相对于显示器屏幕分辨率而言的 em:相对单位,继承父节点(层层继承,传递)基准点为父节点字体的大小,如果自身定义了font-size按自身来计算(浏览器默认字体是16px ...

  8. Oracle 高可用作业测试

    1.RAC 是一种: A 分布式数据库B 内存共享多节点数据库C 能够保护数据安全的架构 2.RAC 和CRS 的区别是: A 前者表示一种架构,后者是一套软件B 前者表示一套软件,后者是一种架构 C ...

  9. Python类(八)-类的起源

    首先用type()看一下类和实例化对象的类型 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" class Person(obj ...

  10. 2015.1.10 解决DataGridView SelectionChanged事件自动触发问题

    DataGridView SelectionChanged事件总是在数据源更改时自动触发,这点很讨厌. 可用CellClick和KeyUp事件和一个函数替代SelectionChanged事件 pri ...