【题目链接】 http://poj.org/problem?id=1795

【题目大意】

  给出n个字符串,求一个最小长度的串,该串包含给出的所有字符串。
  要求长度最小且字典序最小。

【题解】

  dp[i][s]表示包括s集合字符串的第i个字符串为开头的最小值
  从后往前贪心得到最小值,然后从前往后搜索得出最小字典序的答案

【代码】

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
const int N=17,INF=0x3f3f3f3f;
string str[N],ans;
int T,Cas=0,n,dp[N][1<<N],cost[N][N];
void init(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i!=j&&str[i].find(str[j])!=string::npos)str[j]=str[i];
}
}sort(str,str+n);
n=unique(str,str+n)-str;
memset(cost,0,sizeof(cost));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)if(i!=j){
int len=min(str[i].length(),str[j].length());
for(int k=0;k<=len;k++){
if(str[i].substr(str[i].length()-k)==str[j].substr(0,k)){
cost[i][j]=str[i].length()-k;
}
}
}
}
}
void dfs(int id,int s){
if(s==0)return;
string tmp; int nxt=-1;
for(int i=0;i<n;i++)if(i!=id&&(s>>i&1)){
if(dp[id][s]==dp[i][s&~(1<<id)]+cost[id][i]){
string t=str[i].substr(str[id].length()-cost[id][i],str[i].length());
if(nxt==-1||t<tmp){tmp=t;nxt=i;}
}
}ans+=tmp;
dfs(nxt,s&~(1<<id));
}
int main(){
cin>>T;
while(T--){
cin>>n;
for(int i=0;i<n;i++)cin>>str[i];
if(n>1){
init();
for(int i=0;i<=n;i++)fill(dp[i],dp[i]+(1<<n),INF);
for(int i=0;i<n;i++)dp[i][1<<i]=str[i].length();
for(int s=0;s<1<<n;s++){
for(int j=0;j<n;j++)if(dp[j][s]!=INF&&(s>>j&1)){
for(int i=0;i<n;i++)if(!(s>>i&1)){
dp[i][s|1<<i]=min(dp[i][s|1<<i],dp[j][s]+cost[i][j]);
}
}
}int id=0;
for(int i=1;i<n;i++){
if(dp[i][(1<<n)-1]<dp[id][(1<<n)-1])id=i;
}ans=str[id];
dfs(id,(1<<n)-1);
}else ans=str[0];
cout<<"Scenario #"<<++Cas<<":"<< endl;
cout<<ans<<endl<<endl;
}return 0;
}

POJ 1795 DNA Laboratory(状压DP)的更多相关文章

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

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

  2. poj 1795 DNA Laboratory

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

  3. POJ 3254 Corn Fields (状压dp)

    题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...

  4. POJ 3254 - Corn Fields - [状压DP水题]

    题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...

  5. [ An Ac a Day ^_^ ] POJ 3254 Corn Fields 状压dp

    题意: 有一块n*m的土地 0代表不肥沃不可以放牛 1代表肥沃可以放牛 且相邻的草地不能同时放牛 问最多有多少种放牛的方法并对1e8取模 思路: 典型的状压dp 能状态压缩 能状态转移 能状态压缩的题 ...

  6. poj 3254Corn Fields (入门状压dp)

    Farmer John has purchased a lush ≤ M ≤ ; ≤ N ≤ ) square parcels. He wants to grow some yummy corn fo ...

  7. POJ 1684 Corn Fields(状压dp)

    描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ ...

  8. POJ 2923 Relocation(状压DP)题解

    题意:有2辆车运货,每次同时出发,n(<10),各自装货容量c1 c2,问最少运几次运完. 思路:n比较小,打表打出所有能运的组合方式,用背包求出是否能一次运走.然后状压DP运的顺序. 代码: ...

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

    题目链接 POJ 1185 今天艾教留了一大堆线段树,表示做不动了,就补补前面的题.QAQ 这个题,我第一次写还是像前面HDU 2167那样写,发现这次影响第 i 行的还用i-2行那样,那以前的方法就 ...

随机推荐

  1. django文件上传、图片验证码、抽屉数据库设计

    1.Django文件上传之Form方式 settings.py, ALLOWED_HOSTS = ['*'] INSTALLED_APPS = [ 'django.contrib.admin', 'd ...

  2. Limeng:Individual Project: Word frequency program -BUAA Advanced Software Engineering

    11061190-李孟 Implement a console application to tally the frequency of words under a directory (2 mod ...

  3. Eureka 使用Spring cloud config 管理中心启动

    Config 工程创建之后,Eureka就可以使用Git上的配置启动服务了. Git 服务器的搭建这里就不细说了(自行解决),下面是我上传再git的配置文件: 创建EurekaServer项目(eur ...

  4. 搭建springmvc项目404,没扫描到包

    搭建简单项目完成之后,曾经出现过一个问题 跳转报了404,控制台忘了没留啊... 反正意思就是说我配置有问题,导致没有扫描到注释的类 <context:component-scan base-p ...

  5. javascript replace 替换全部字符

    $("#pageNote").html($("#note3").val().replace(/ /g, "").replace(/<b ...

  6. Linux下从零开始部署和使用Jaeger

    最近在折腾Jaeger,Jaeger官网都是介绍如何通过Docker部署,二进制部署文档基本没有(已咨询过作者,作者说没文档!你参考Docker自己部署好了!!!),所以打算写一篇Linux部署. J ...

  7. 雅礼集训 Day1 T2 折射

    折射 题目描述 小\(\mathrm{Y}\)十分喜爱光学相关的问题,一天他正在研究折射. 他在平面上放置了\(n\)个折射装置,希望利用这些装置画出美丽的折线. 折线将从某个装置出发,并且在经过一处 ...

  8. Codeforces Round #324 (Div. 2) D

    D. Dima and Lisa time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  9. Linux临时增加swap空间

    linux临时增加swap空间:step 1: #dd if=/dev/zero of=/home/swap bs=1024 count=500000 注释:of=/home/swap,放置swap的 ...

  10. VMware DRS概述及功能

    通过动态分配和平衡计算资源,使您的 IT 基础架构与业务目标一致.VMware Distributed Resource Scheduler (DRS) 可持续监控所有资源池的利用率,并根据业务需求在 ...