题意:有一些小孩(至少两个)围成一圈,有 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. UVA 11176 Winning Streak

    #include <iostream> #include <stdio.h> #include <cstring> #define N 501 using name ...

  2. C#获取堆栈信息,输出文件名、行号、函数名、列号等

    命名空间:System.Diagnostics 得到相关信息: StackTrace st = new StackTrace(new StackFrame(true));StackFrame sf = ...

  3. 学习动态性能表(10)--v$session_longops

    学习动态性能表 第十篇--V$SESSION_LONGOPS  2007.6.7 本视图显示运行超过6秒的操作的状态.包括备份,恢复,统计信息收集,查询等等. 要监控查询执行进展状况,你必须使用cos ...

  4. vue.js初学(二)

    1:构造器 var app = new Vue ( { //选项 }) 注意点: (1) 之后会经常用vm代表Vue实例 (2)实例化之后 需要传入一个选项对象,它可以包括数据.模板.挂载元素.方法. ...

  5. js中call apply方法的使用介绍

    js call call 方法 请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg2[, ...

  6. Azure CLI的Query

    Azure CLI 2.0是基于Python的命令行.其命令直观,使用非常方便. 其输出有四种模式: --output -o : Output format. Allowed values: json ...

  7. 解决IDEA输入法不跟随

    1.关掉idea后在idea的安装路径下把jre64文件夹删掉,或者重命名也行,如把jre64改成jre642: 2.升级jdk版本至jdk 8u45以上 3.把Java安装路径下的jre文件拷贝到I ...

  8. uptime命令查看系统启动时间和运行时间、查看linux系统负载

    1.uptime命令输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.00 2.查看/proc/uptime文件计算 ...

  9. Web项目安全相关博客日志大集合(仅供学习及参考)

    强制使用HTTPS --- Tomcat篇 ---通过在tomcat/conf/web.xml中进行配置,从而将http自动转为https.(即强制HSTS)http://blog.csdn.net/ ...

  10. Angular学习笔记-问题列表(2016.4.7)

    1.控制器中注入服务时,出问题,当前pageInit这个页面无法加载,页面无报错信息. var homepage = angular.module('homepage'); homepage.cont ...