UVa 1204 Fun Game (状压DP)
题意:有一些小孩(至少两个)围成一圈,有 n 轮游戏,每一轮从某个小孩开始往左或者往右伟手帕,拿到手帕写上自己的性别(B,G),然后以后相同方向给下一个。
然后在某个小孩结束,给出 n 轮手帕上的序列,求最少有多少个小孩。
析:很容易知道是状压DP,也很容易写出状态方程,dp[s][i][j] 表示 已经选择了 s 的手帕,然后第一个是 i,最后一个是 j,然后再转移,很简单,但是,
这样时间复杂度可能是 n^4*2^n ,太大了,所以必须减少一维状态,dp[s][i] 时间复杂度是 n^2*2^n,可以接受,表 已经选择了 s 的手帕,然后最后一个是 i,
并且规定第 1 个序列是最正向的在最前面。
代码如下:
#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 = 10000 + 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][2];
string str[16][2];
int overlap[16][16][2][2]; int solve(const string &a, const string &b){
for(int i = 1; i < a.size(); ++i){
if(i + b.size() <= a.size()) continue;
bool ok = true;
for(int j = i; j < a.size(); ++j)
if(a[j] != b[j-i]){ ok = false; break; }
if(ok) return a.size() - i;
}
return 0;
} int main(){
ios_base::sync_with_stdio(false);
while(cin >> n && n){
for(int i = 0; i < n; ++i){
cin >> str[i][0];
str[i][1] = str[i][0];
reverse(str[i][1].begin(), str[i][1].end());
}
vector<string> v[2];
for(int i = 0; i < n; ++i){
bool ok = true;
for(int j = 0; j < n; ++j){
if(i == j) continue;
if(str[j][0].size() < str[i][0].size()) continue;
if(str[j][0].find(str[i][0]) != string::npos || str[j][1].find(str[i][0]) != string::npos){
ok = false; break;
}
}
if(ok) v[0].push_back(str[i][0]), v[1].push_back(str[i][1]);
}
if(v[0].empty()) v[0].push_back(str[0][0]), v[1].push_back(str[0][1]);
n = v[0].size();
int all = 1<<n;
for(int i = 0; i < n; ++i) for(int x = 0; x < 2; ++x)
for(int j = 0; j < n; ++j) for(int y = 0; y < 2; ++y)
overlap[i][j][x][y] = solve(v[x][i], v[y][j]); memset(dp, INF, sizeof dp);
dp[1][0][0] = v[0][0].size();
--all;
for(int i = 1; i < all; ++i)
for(int j = 0; j < n; ++j) for(int x = 0; x < 2; ++x){
if(dp[i][j][x] == INF) continue;
for(int k = 1; k < n; ++k){
if(i&(1<<k)) continue;
for(int y = 0; y < 2; ++y)
dp[i|(1<<k)][k][y] = min(dp[i|(1<<k)][k][y], dp[i][j][x] + (int)v[0][k].size() - overlap[j][k][x][y]);
}
} int ans = INF;
for(int i = 0; i < n; ++i)
for(int x = 0; x < 2; ++x){
if(dp[all][i][x] == INF) continue;
ans = min(ans, dp[all][i][x] - overlap[i][0][x][0]);
} if(1 == ans) ans = 2;
cout << ans << endl;
}
return 0;
}
UVa 1204 Fun Game (状压DP)的更多相关文章
- UVa 11825 Hackers' Crackdown (状压DP)
题意:给定 n 个计算机的一个关系图,你可以停止每台计算机的一项服务,并且和该计算机相邻的计算机也会终止,问你最多能终止多少服务. 析:这个题意思就是说把 n 台计算机尽可能多的分成一些组,使得每组的 ...
- UVa 1252 Twenty Questions (状压DP+记忆化搜索)
题意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同), 最小需要多少次询问? 析:我们假设心中想的那个物 ...
- UVA - 1252 Twenty Questions (状压dp+vis数组加速)
有n个物品,每个物品有m个特征.随机选择一个物品让你去猜,你每次可以询问一个特征的答案,问在采取最优策略时,最坏情况下需要猜的次数是多少. 设siz[S]为满足特征性质集合S的特征的物品总数,dp[S ...
- UVA 11825 Hackers’ Crackdown 状压DP枚举子集势
Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...
- UVA 1412 Fund Management (预处理+状压dp)
状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...
- 状压DP UVA 10817 Headmaster's Headache
题目传送门 /* 题意:学校有在任的老师和应聘的老师,选择一些应聘老师,使得每门科目至少两个老师教,问最少花费多少 状压DP:一看到数据那么小,肯定是状压了.这个状态不好想,dp[s1][s2]表示s ...
- UVa 11825 (状压DP) Hackers' Crackdown
这是我做状压DP的第一道题,状压里面都是用位运算来完成的,只要耐下心来弄明白每次位运算的含义,还是容易理解的. 题意: 有编号为0~n-1的n台服务器,每台都运行着n中服务,每台服务器还和若干台其他服 ...
- UVA - 1252 Twenty Questions (状压dp)
状压dp,用s表示已经询问过的特征,a表示W具有的特征. 当满足条件的物体只有一个的时候就不用再猜测了.对于满足条件的物体个数可以预处理出来 转移的时候应该枚举询问的k,因为实际上要猜的物品是不确定的 ...
- 状压dp的题目列表 (一)
状压dp的典型的例子就是其中某个数值较小. 但是某个数值较小也不一定是状压dp,需要另外区分的一种题目就是用暴力解决的题目,例如UVA818 紫书215 题目列表: ①校长的烦恼 UVA10817 紫 ...
随机推荐
- HihoCoder 1190连通性·四
连通性·四 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho从约翰家回到学校时,网络所的老师又找到了小Hi和小Ho. 老师告诉小Hi和小Ho:之前的分组出了 ...
- .net core结合Consul集群&Docker实现服务治理
实战中的asp.net core结合Consul集群&Docker实现服务治理 https://www.cnblogs.com/guolianyu/p/9614050.html 0.目录 整体 ...
- dbca 快速健建库
[oracle@e0946877f272 ~]$ dbca -silent -createDatabase -templateName $ORACLE_HOME/assistants/dbca/tem ...
- 还是畅通工程(peime算法最小生成树)
个人心得:就是最小生成树的运用,还是要理解好每次都是从已搭建好的生成树里面选择与她的补集中最短距离,所以那个book数组的更新 需要好生体会.不过还是有缺陷,算法的复杂度为O(n^2),看介绍说用优先 ...
- linkedLoop
public class linkQueue <E>{ private class Node<E>{ E e; Node<E> next; public Node( ...
- [转]实现微信浏览器内打开App Store链接
微信浏览器是不支持打开App Store 页面的,不知道微信为什么这么做.比如你页面写 <a href=”http://itunes.apple.com/us/app/id399608199″& ...
- MyEclipse安装jbpm插件
介绍如何在MyEclipse8.6里安装jbpm插件. 工具/原料 MyEclipse8.6 jbpm-4.4.rar 方法/步骤 1 下载jbpm包并解压 下载最新的jbpm包,本文以jbpm4.4 ...
- MySQL 数据库备份种类以及常用备份工具汇总
1,数据库备份种类 按照数据库大小备份,有四种类型,分别应用于不同场合,下面简要介绍一下: 1.1完全备份 这是大多数人常用的方式,它可以备份整个数据库,包含用户表.系统表.索引.视图和存储过程等所有 ...
- Java基础--枚举Enum
Java中的枚举是一种特殊的类,可以将一组固定常量的集合组成一种类型,使用方便且类型安全.使用enum关键字定义. enum类型父类为Enum,通过Enum.class可见Enum为抽象类,实现了Co ...
- java反射专题二
一丶Class中常用方法详解 1)getFields() 只能获取到运行时类中及其父类中声明为public的属性 2)getDeclaredFields() 获取运行时类本身声明的所有属性 3)get ...