Codeforces 482C Game with Strings(dp+概率)
题目链接:Codeforces 482C Game with Strings
题目大意:给定N个字符串,如今从中选定一个字符串为答案串,你不知道答案串是哪个。可是能够通过询问来确定,
每次询问一个位置上字符为多少。
如今你询问的每一个位置的概率是同样的,(问过的位置不会再问)。求询问次数的期
望。
解题思路:由于字符串长度不会大于20。所以用二进制表示询问了哪些位置。C[s]即为询问s的位置能够确定多少个字
符串。
这步不能通过枚举s,然后推断处理,复杂度为o(2^20 * 20 * 50),太高。
能够通过枚举两个字符串,推断它们哪些
位置同样,则在相应的s状态下标记这两个字符串无法被区分,最后在遍历一遍s状态递推一次,由于假设s1状态包括s2
状态。那么s2状态下不能区分的字符串相同在s1状态无法区分。
确定每一个状态下可区分字符串的个数之后就好做了,dp[s]表示移动到状态s后还要继续推断的概率,不用继续推断的直
接加到相应的步数中。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, int> pii;
const int maxs = (1<<20) + 5;
const int maxn = 55;
const int maxm = 25;
const ll x = 123;
int N, L, T, C[maxs];
ll D[maxs];
char str[maxn][maxm];
inline int idx(char ch) {
if (ch >= 'a' && ch <= 'z')
return ch - 'a';
return ch - 'A' + 26;
}
inline int bitcount(ll x) {
return x == 0 ? 0 : bitcount(x>>1) + (x&1);
}
void init () {
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%s", str[i]);
L = strlen(str[0]);
T = (1<<L);
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
int s = 0;
for (int k = 0; k < L; k++)
s |= (str[i][k] == str[j][k]) << k;
D[s] |= (1LL<<i) | (1LL<<j);
}
}
for (int i = T-1; i; i--) {
for (int j = 0; j < L; j++)
if (i&(1<<j))
D[i^(1<<j)] |= D[i];
C[i] = N - bitcount(D[i]);
}
}
double dp[maxs], p[100];
int main () {
init();
if (N == 1) {
printf("%.9lf\n", 0.0);
return 0;
}
dp[0] = 1;
for (int u = 0; u < T; u++) {
int bit = bitcount(u), sum = N - C[u];
double mv = dp[u] / (L - bit);
if (N == C[u]) continue;
for (int i = 0; i < L; i++) {
if (u&(1<<i))
continue;
int s = u | (1<<i);
double a = 1.0 * (C[s] - C[u]) / sum;
dp[s] += mv * (1 - a);
p[bit+1] += mv * a;
}
}
/*
double sum = 0;
for (int u = 1; u <= L; u++) {
printf("%lf\n", p[u]);
sum += p[u];
}
printf("%lf!\n", sum);
*/
double ans = 0;
for (int i = 1; i <= L; i++)
ans += p[i] * i;
printf("%.9lf\n", ans);
return 0;
}
Codeforces 482C Game with Strings(dp+概率)的更多相关文章
- CodeForces 482C Game with Strings
意甲冠军: n一定长度m串 隐藏的字符串相等的概率 然后,你猜怎么着玩家隐藏的字符串 的询问字符串的一个位置 再不断的知道一些位置后 游戏者就能够确定藏起来的串是什么 问 游戏者的期望步 ...
- Codeforces 360C Levko and Strings dp
题目链接:点击打开链接 题意: 给定长度为n的字符串s,常数k 显然s的子串一共同拥有 n(n-1)/2 个 要求找到一个长度为n的字符串t,使得t相应位置的k个子串字典序>s #include ...
- codeforces 696B B. Puzzles(树形dp+概率)
题目链接: B. Puzzles time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- UVA 11427 Expect the Expected(DP+概率)
链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 [思路] DP+概率 见白书. [代码] #include&l ...
- tyvj P1864 [Poetize I]守卫者的挑战(DP+概率)
P1864 [Poetize I]守卫者的挑战 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜 ...
- Codeforces 385B Bear and Strings
题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...
- [LnOI2019]加特林轮盘赌(DP,概率期望)
[LnOI2019]加特林轮盘赌(DP,概率期望) 题目链接 题解: 首先特判掉\(p=0/1\)的情况... 先考虑如果\(k=1\)怎么做到\(n^2\)的时间复杂度 设\(f[i]\)表示有\( ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- Codeforces Round #358 (Div. 2) D. Alyona and Strings dp
D. Alyona and Strings 题目连接: http://www.codeforces.com/contest/682/problem/D Description After return ...
随机推荐
- c:forEach实现换行
Map<String,String> map = new TreeMap<String,String>(); map.put("key1", "v ...
- Thinkphp学习笔记2-
因为操作方法就是控制器的一个方法,所以遇到有和系统的关键字冲突的方法可能就不能定义了,这个时候我们可以设置操作方法的后缀来解决,例如: 'ACTION_SUFFIX' => 'Action', ...
- [JS Compose] 7. Ensure failsafe combination using monoids
monoids is a semi-group with a neutral element. A semigroup, it does not have an element to return s ...
- onvif杂项
onvif规范 中文介绍 什么是ONVIF ? ONVIF规范描述了网络视频的模型.接口.数据类型以及数据交互的模式.并复用了一些现有的标准,如WS系列标准等.ONVIF规范的目标是实现一个网络视频框 ...
- Unity Game Starter Kit for Windows Store and Windows Phone Store games
原地址:http://digitalerr0r.wordpress.com/2013/09/30/unity-game-starter-kit-for-windows-store-and-window ...
- SpriteKit改变Node锚点其物理对象位置不对的解决
在创建Node的物理对象后,默认情况下物理对象和Node的实际边界相应的非常好,由于此时Node的默认锚点是当中心位置即(0.5,0.5),只是假设我们改变了Node的锚点,就会发现其物理边界还是保持 ...
- oracle 存储过程 示例
oracle 存储过程 示例 CreationTime--2018年9月4日09点49分 Author:Marydon 1.情景展示 对VIRTUAL_QRCODELOG表的静态二维码,动态二维码 ...
- sparkContext 读取hdfs文件流程及分片机制
- 【jquery操作cookie】JQuery中$.cookie()方法的使用(同名cookie会覆盖)
jquery.cookie.js插件: <script type="text/javascript" src="js/jquery-1.6.2.min.js&quo ...
- 安装Ubuntu 13.04后要做的六件事
2013-05-07 09:23 最新版本的Ubuntu已经新鲜出炉:Ubuntu 13.04,代号为Raring Ringtail.作为幕后开发Ubuntu Linux的公司,Canonica ...