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 ...
随机推荐
- mysql5.0.x统计每秒增删改查替换数及系统每秒磁盘IO
转载自:http://blog.chinaunix.net/uid-9370128-id-393082.html 1. mysql 5.0.x 统计每秒增,删,改,查,替换数 mysql 的show ...
- Linux网络编程之聊天程序(TCP协议之select)
服务器端:server.c #include <stdio.h> #include <stdlib.h> #include <errno.h> #include & ...
- Linux阅读笔记(一)
1.关机命令 shutdown -h now 马上关机 shutdown -r now 马上重新启动 reboot ...
- Cocos2d-x 3.2 Lua演示样例 AssetsManagerTest(资源管理器)
Cocos2d-x 3.2 Lua演示样例 AssetsManagerTest(资源管理器) 本篇博客介绍Cocos2d-x 为我们提供的一个类--AssetsManager在Lua中的使用样例,效果 ...
- ES6 class 基本使用
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- JDBC 可更新和对更新敏感的结果集
public class OtherApi { /** * @param args * @throws SQLException * @throws Interr ...
- 解析theme()
drupal_render()只是对theme()的调用做了包装,真正做任务的还是theme(). function theme($hook, $variables = array()) { ... ...
- js特效,轻松实现内容的无缝平滑滚动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- C++:模板友元
模板友元函数在类内声明类外定义时都必须加模板前缀,另外模板要写在一个文件内 // generates undefined error for the operator<< function ...
- WEP无线网络密码破解
一,五分钟实现无线WEP入侵的特点: 众所周知WEP加密入侵需要用户通过监听等方法接收到足够数量的WEP验证数据包,然后通过分析软件使用暴力破解的方法穷举法还原出WEP加密密钥的明文信息.五分钟实现无 ...