Codeforces 482C Game with Strings(dp+概率)
来源:互联网 发布:python编译器是什么 编辑:程序博客网 时间:2024/04/30 08:35
题目链接: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;}
0 0
- Codeforces 482C Game with Strings(dp+概率)
- codefroces 482C Game with Strings (概率dp神题)
- CodeForces - 482C Game with Strings(状压DP,期望)
- Codeforces 482C. Game with Strings 状压DP
- CodeForces 482C Game with Strings
- Codeforces 482C Game with strings
- Codeforces 482C Game with Strings
- codeforces 482C Game with Strings
- 482 C. Game with Strings
- 【Codeforces Round 275 (Div 2)E】【状压DP 概率DP求期望 线性相加思想 二进制系统函数】Game with Strings n个串目标串随机猜位置随机的猜出答案的期望步
- 概率DP Spoj4060 game with probability Problem
- codeforces 601C (概率DP)
- codeforces 28C (概率DP)
- CF482C Game with Strings(期望dp+状压dp)
- Codeforces 280C Game on Tree 概率dp 树上随机删子树 求删完次数的期望
- CodeForces 280C Game on Tree 概率
- Codeforces 360C Levko and Strings (dp)
- Codeforces 360C Levko and Strings dp
- 排序算法之插入排序
- 排序算法之选择排序
- 交换a,b两个元素的值
- 大家不要再用流量软件了,害人呀!
- ubuntu安装libxml2
- Codeforces 482C Game with Strings(dp+概率)
- Spring Security3.1 配置说明
- Android开发中:如何判断某Application和Activity的存在性
- android 机型适配问题
- 简单理解WCF的安全
- Visual C++ 2010新功能之auto关键字
- #!/usr/bin/env python与#!/usr/bin/python的区别
- IOS线程数据篇6之数据持久化plist、preference、NSKeyedArchiver(NSCoding)、Core Data、SQLite3
- 每个程序员都该知道的10大编码原则