题意:有一些小孩(至少两个)围成一圈,有 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. HDU4940 Destroy Transportation system(有上下界的最大流)

    Problem Description Tom is a commander, his task is destroying his enemy’s transportation system. Le ...

  2. 1110. Complete Binary Tree (25)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  3. axios 请求报错

    报错如下: 解决:axios的请求配置中的baseURL配置错误,修改好即可. 报错前: baseURL: "192.168.30.220:3000", 解决后:baseURL: ...

  4. [Unity3D]关于U3D贴图格式压缩

    http://blog.sina.com.cn/s/blog_5b6cb9500102vi6i.html 因为有不少人都问过我压缩格式的问题,今天飞哥又重新提醒了一次.整理一下发个贴,以供大家查阅和讨 ...

  5. 关于讯飞语音SDK开发学习

    前奏,浑浑噩噩已经工作一年多,这一年多收获还是挺多的.逛园子应该有两年多了,工作后基本上是天天都会来园子逛逛,园子 里还是有很多牛人写了一些不错的博客,帮我解决很多问题.但是一直没写过博客,归根到底一 ...

  6. 【转】JVM可视化工具插件---Visual GC

    Visual GC是一个Java 内存使用分析与GC收集的可视化工具插件 <插件下载> 一:整个区域分为三部分:spaces.graphs.histogram 1 spaces区域:代表虚 ...

  7. Azure VM复制

    目前Azure上复制VM可以有多种方法: 1 创建User Image,可以快速复制多台VM,但目前托管磁盘只支持Generalized的模式,需要对User和配置进行重置. 2 从VHD或托管磁盘复 ...

  8. Azure上批量创建OS Disk大于30G的Linux VM

    Azure上VM的OS盘的大小在创建时是固定的.Windows是127G,Linux是30G.如果需要批量创建的VM的OS Disk有更大的容量.可以考虑用下面的方法实现. 1 创建一台有Data-d ...

  9. POJ3321(dfs序列+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25711   Accepted: 7624 Descr ...

  10. java继承捡漏

    总结:老师真是的.讲课的时候,觉得打的比喻特别有趣,结果啊不晓得他是良苦用心.知识就是包含在里面 继承:子类继承父类,子类就可以把父类的东西拿来用 这体现在程序运行时,你会发现,子类对象可以调用父类的 ...