CF482C Game with Strings
题意
你和你的朋友玩一个游戏,游戏规则如下。
你的朋友创造 n 个长度均为 m 的不相同的字符串,然后他随机地选择其中一个。他选择这些字符串的概率是相等的,也就是说,他选择 n 个字符串中的每一个的概率是 1/n 。你想猜猜你的朋友选择了哪个字符串。
为了猜到你的朋友选择了哪个字符串,你可以问他问题,形式如下:字符串中第 pos 个字符是什么?当问题的答案为唯一标识字符串时,我们认为这个字符串是猜测的。在字符串被猜测后,你将停止提问。
你没有一个特殊的策略,所以你每次可能会等概率的问任何一个你从没提过的位置。你的任务是确定你猜到你的朋友选的字符串所需次数的期望。
输入格式
第一行包括一个数字 n 。接下来 n 行,每行一个字符串,表示你朋友创造出的字符串。除此之外,所有字符的长度是相同的,在1~20之间。
输出格式
输出期望。答案保留九位小数。误差在\(10^-9\)以内.
输入输出样例
输入样例#1:
2
aab
aac
输出样例#1:
2.000000000000000
输入样例#2:
3
aaA
aBa
Caa
输出样例#2:
1.666666666666667
输入样例#3:
3
aca
vac
wqq
输出样例#3:
1.000000000000000
搞了一上午
撞鸭 + 期望
我们预处理出unf[i]表示在状态为i的情况下有哪些串是符合的
unf[i]我们可以通过枚举两个串来计算他们相同的位置
然后我们再从大到小更新unf
我们还可以求出来Num[i]表示在状态为i的情况下有多少串是符合的
f[i] 表示状态为i时距离确定的期望
然后就可以从后向前转移了
状态转移方程\(f[i] = \sum_{j = 1 \&\& (! i | (1 << (j - 1)))}^{n}{\frac{f[i]|1<<(j-1)]}{tot} * \frac{Num[i]|1<<(j-1)}{Num[i]}} + 1\)
tot表示的是状态i中还有多少个位置没问
解释下\(\frac{Num[i||1<<(j-1)}{Num[i]}\)的意思
设\(j = i | (1 << (j - 1)\)
num[j] 一定不大于 num[i]
因为是j向i转移不好考虑
换成i向j转移思考,i有num[j]种选择可以变成j的样子
但是有(num[i]-num[j])的选择是可以直接分辨出答案来的
所以由j向i转移的时候就只需要考虑到那num[j]种情况,所以只有\(num[j]/num[i]\)的概率可以继续转移
余下的\((num[i]-num[j])/num[j]\)就是已经确定的那些
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
# define int long long
const int M = 55 ;
const int N = (1 << 20) + 5 ;
using namespace std ;
int m , n ;
char s[M][M] ;
int unf[N] , Num[N] ;
double f[N] ;
# undef int
int main() {
# define int long long
scanf("%lld",&m) ;
for(int i = 1 ; i <= m ; i ++) scanf("%s",s[i] + 1) ;
n = strlen(s[1] + 1) ;
for(int i = 1 ; i <= m ; i ++)
for(int j = i + 1 , sit ; j <= m ; j ++) {
sit = 0 ;
for(int k = 1 ; k <= n ; k ++)
if(s[i][k] == s[j][k])
sit |= (1LL << (k - 1)) ;
unf[sit] |= (1LL << (i - 1)) ;
unf[sit] |= (1LL << (j - 1)) ;
}
for(int i = (1 << n) - 1 ; i >= 1 ; i --)
for(int j = 1 ; j <= n ; j ++)
if(i & (1 << (j - 1)))
unf[i ^ (1 << (j - 1))] |= unf[i] ;
for(int i = 0 ; i < (1 << n) ; i ++)
for(int j = 1 ; j <= m ; j ++)
if(unf[i] & (1LL << (j - 1)))
Num[i] ++ ;
for(int i = (1 << n) - 2 , tot ; i >= 0 ; i --) {
if(!Num[i]) continue ;
tot = n ;
for(int j = 1 ; j <= n ; j ++)
if(i & (1 << (j - 1))) --tot ;
for(int j = 1 ; j <= n ; j ++) {
if(i & (1 << (j - 1))) continue ;
f[i] += f[i | (1 << (j - 1))] / (double)tot * ((double)Num[i | (1 << (j - 1))] / (double)Num[i]) ;
}
f[i] += 1.0 ;
}
printf("%.10lf\n",f[0]) ;
return 0 ;
}
CF482C Game with Strings的更多相关文章
- CF482C Game with Strings (状压DP+期望DP)
题目大意:甲和乙玩游戏,甲给出n(n<=50)个等长的字符串(len<=20),然后甲选出其中一个字符串,乙随机询问该字符串某一位的字符(不会重复询问一个位置),求乙能确定该串是哪个字符串 ...
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [LeetCode] Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
随机推荐
- Codeforces Round #259 (Div. 2) D
D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes i ...
- noip模拟赛 道路分组
分析:因为每一组编号都是连续的嘛,所以能分成一组的尽量分,每次加边后dfs判断一下1和n是否连通.有向图的判连通没有什么很快的方法,特别注意,并查集是错的!这个算法可以得到60分. 事实上每一次都不需 ...
- eclipse常用使用指南
1.eclipse添加new/删除/一个tomcat. 步骤:window->preference->server->runtime environment.remove掉,再重新n ...
- apache.commons.lang.StringUtils 使用心得
原文:http://blog.csdn.net/ye_sheng/article/details/48101901?ref=myread 在Java中我们用的最多的类应该就是String了.对于Str ...
- 集成学习(ensemble method)--基于树模型
bagging方法(自举汇聚法 bootstrap aggregating) boosting分类:最流行的是AdaBoost(adaptive boosting) 随机森林(random fores ...
- java MAT 分析
java MAT 分析 http://blog.csdn.net/qeqeqe236/article/details/43577857 https://www.cnblogs.com/AloneSwo ...
- ubuntu VSFTPD搭建FTP服务器 提示530错误
配置完 vsftpd ,发现不能登录,提示 530 错误.解决方法如下: sudo rm /etc/pam.d/vsftpd 注:因为 ubuntu 启用了 PAM,所在用到 vsftp 时需要用到 ...
- Eclipse中的Web项目自己主动部署到Tomcat以及怎样在Eclipse中使用My Eclipseproject
我是一个新手学习Java,servlet和Jsp. 痛苦的是我时候一个.net程序猿,习惯了微软的VS IDE一切都是封装好的.傻瓜式的使用, 不须要关心内部实现. 悲催的是我看到资料都是My Ecl ...
- CodeForces484A Bits(贪心)
CodeForces484A Bits(贪心) CodeForces484A 题目大意:给出范围[A.B].期望你给出某个数X满足X属于[A,B],而且X转成二进制的1的个数最多.假设有多个给出最小的 ...
- 通过android XML 创建图形,降低对美工的依赖
在开发中总会须要自己定义各种View的样式,假设总是依赖美工作图弄出须要的UI样式图片(比方:一个button要选中和默认两张图片),不但时间上会浪费.往往也会有适配问题. 尽管能够通过.9图来解决一 ...