题意:有一些小孩(至少两个)围成一圈,有 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)的更多相关文章

  1. UVa 11825 Hackers' Crackdown (状压DP)

    题意:给定 n 个计算机的一个关系图,你可以停止每台计算机的一项服务,并且和该计算机相邻的计算机也会终止,问你最多能终止多少服务. 析:这个题意思就是说把 n 台计算机尽可能多的分成一些组,使得每组的 ...

  2. UVa 1252 Twenty Questions (状压DP+记忆化搜索)

    题意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同), 最小需要多少次询问? 析:我们假设心中想的那个物 ...

  3. UVA - 1252 Twenty Questions (状压dp+vis数组加速)

    有n个物品,每个物品有m个特征.随机选择一个物品让你去猜,你每次可以询问一个特征的答案,问在采取最优策略时,最坏情况下需要猜的次数是多少. 设siz[S]为满足特征性质集合S的特征的物品总数,dp[S ...

  4. UVA 11825 Hackers’ Crackdown 状压DP枚举子集势

    Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...

  5. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

  6. 状压DP UVA 10817 Headmaster's Headache

    题目传送门 /* 题意:学校有在任的老师和应聘的老师,选择一些应聘老师,使得每门科目至少两个老师教,问最少花费多少 状压DP:一看到数据那么小,肯定是状压了.这个状态不好想,dp[s1][s2]表示s ...

  7. UVa 11825 (状压DP) Hackers' Crackdown

    这是我做状压DP的第一道题,状压里面都是用位运算来完成的,只要耐下心来弄明白每次位运算的含义,还是容易理解的. 题意: 有编号为0~n-1的n台服务器,每台都运行着n中服务,每台服务器还和若干台其他服 ...

  8. UVA - 1252 Twenty Questions (状压dp)

    状压dp,用s表示已经询问过的特征,a表示W具有的特征. 当满足条件的物体只有一个的时候就不用再猜测了.对于满足条件的物体个数可以预处理出来 转移的时候应该枚举询问的k,因为实际上要猜的物品是不确定的 ...

  9. 状压dp的题目列表 (一)

    状压dp的典型的例子就是其中某个数值较小. 但是某个数值较小也不一定是状压dp,需要另外区分的一种题目就是用暴力解决的题目,例如UVA818 紫书215 题目列表: ①校长的烦恼 UVA10817 紫 ...

随机推荐

  1. idea 创建maven工程(入门)

    转:http://blog.csdn.net/qq_32588349/article/details/51461182 1. 下载Maven 官方地址:http://maven.apache.org/ ...

  2. centos7 showdoc 手动安装部署

    1.从 https://github.com/star7th/showdoc 下载压缩包: 2.解压文件,然后将showdoc-master下面的文件上传至服务器对应的文件下面: 3.在浏览器中输入 ...

  3. JavaWeb框架_Struts2_(四)----->表达式语言OGNL

      2. 表达式语言OGNL 2.1 OGNL简介 OGNL(Object-Graph Navigation Language)对象图导航语言的缩写,OGNL是一种表达式语言(Expression L ...

  4. GWT嵌入纯HTML页面

    众所周知,gwt页面是java代码所写,不存在html页面直接作用于gwt面板中.不过gwt也倒是提供了一些可用的功能,比如frame,这个是UI中的一个,内部可以设置URL,但是经过我测试后发现,这 ...

  5. [转载]Sleep(x)的使用和sleep(0)作用解析

    链接:http://blog.csdn.net/m_leonwang/article/details/28434383 假设现在是 2012-12-16 3:37:40,如果我调用一下 Thread. ...

  6. openwrt 按下回车才能显示图标信息

    如题所示,openwrt启动后,手动才能按下系统图标和信息. 如何却掉这个手动选项呢? 修改文件/SISP-L26.7.8-OpenWrt/build_dir/target-arm_uClibc-0. ...

  7. php用zendstudio建立wsdl

    首先,新建的时候要选择soap,然后deocument和rpc都可以. 类和方法的页面: <?php //发货接口 class test{ function send_do_delivery($ ...

  8. UE4流关卡

    转自:http://blog.ch-wind.com/ue4-level-streaming/ 流关卡可以使得关卡内容只在玩家“需要”的时候才加载,在很多游戏中都有使用这个技术. 当前UE4版本4.1 ...

  9. 开发环境入门 linux基础 (部分)正则表达式 grep sed

    /etc/profile /etc/bashrc  .变量添加到shell环境中,永久生效. /root/.bashrc /root/.bash_profile 正则表达式 定义:正则就是用一些具有特 ...

  10. qt安装必要的库 qt开源安装包下载

    yum install mesa-libGL-devel mesa-libGLU-devel #yum install freeglut-devel http://www.qt.io/download ...