【题目链接】 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. 添加selenium对应的jar包至pom.xml

    1.进入https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java,点开相应的版本 2.复制图中选中的代码,粘贴至 ...

  2. redis的socket event loop

    很早之前就因为nosql就听说了redis,直到去年才真正去了解,只能说相见恨晚. 因为数据库相关,我以为这应该是个庞然大物,万万没想到,源码不到2M,所以,我不知道该说啥了... 还是来点靠谱的: ...

  3. over窗口函数进阶

    over窗口函数的其他灵活的用法.即,统计当前行的前N行及后N行数据.转自:https://blog.csdn.net/ck3207/article/details/84954511 先来看一下数据的 ...

  4. Python中的单元测试模块Unittest快速入门

    前言 为什么需要单元测试? 如果没有单元测试,我们会遇到这种情况:已有的健康运行的代码在经过改动之后,我们无法得知改动之后是否引入了Bug.如果有单元测试的话,只要单元测试全部通过,我们就可以保证没有 ...

  5. POJ 2074 | 线段相交

    #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #defi ...

  6. mysql慢查询工具

    GeorgeHao 安装过程: [root@localhost-centos6 ~]# wget percona.com/get/pt-query-digest [root@localhost-cen ...

  7. VMware Fault Tolerance 概述及功能

    VMware Fault Tolerance - 为您的应用程序提供全天候可用性 通过为虚拟机启用 VMware Fault Tolerance,最大限度地延长数据中心的正常运行时间,减少停机管理成本 ...

  8. 封装removeClass()

    <div class="box haha xixi">123</div> <script> function removeClass(eleme ...

  9. 转:Java NIO(3)

    要想讲清楚nio的原理和它的优点得先清楚Java应用程序的文件读写原理和虚拟内存的原理.Java文件读取原理可参见如下图: 当应用程序需要读取文件的时候,内核首先通过DMA技术将文件内容从磁盘读入内核 ...

  10. 获得touch事件,jquery绑定事件监听器,ios设备上打开touch状态以响应focus,active等伪类

    2. 默认的监听方式 document.addEventListener('touchstart', function(){ alert('hello'); }, false); 使用jquery时 ...