题意

你和你的朋友玩一个游戏,游戏规则如下。

你的朋友创造 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的更多相关文章

  1. CF482C Game with Strings (状压DP+期望DP)

    题目大意:甲和乙玩游戏,甲给出n(n<=50)个等长的字符串(len<=20),然后甲选出其中一个字符串,乙随机询问该字符串某一位的字符(不会重复询问一个位置),求乙能确定该串是哪个字符串 ...

  2. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  3. 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 ...

  4. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  6. [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 ...

  7. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  8. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  9. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

随机推荐

  1. bzoj4553 [Tjoi2016&Heoi2016]序列 树状数组(区间最大值)+cqd

    [Tjoi2016&Heoi2016]序列 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1006  Solved: 464[Submit][ ...

  2. msp430入门编程11

    msp430中C语言的模块化头文件及实现11 msp430中C语言的模块化头文件及库文件12 msp430入门学习 msp430入门编程

  3. POJ 3276 Face The Right Way【枚举】

    题意: N头牛站成一条线,分别朝向前后两个方向,机器可以使连续K头牛同时改变方向,要求所有牛最终朝向前方,问机器操作次数的最小值及此时的最小K值. 分析: 第一眼看感觉是二分搜索K,再仔细读题, pl ...

  4. React Native学习(五)—— 使用插件react-native-scrollable-tab-view

    本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...

  5. Jquery第四课 Javascript中this的使用方法

    thiskeyword的使用 在JavaScript中使用thiskeyword表示调用方法的对象,这与大部分面向对象语言是一样的.可是因为call.apply.bind等函数的影响.我们能够改变th ...

  6. HDU-4643-GSM(DFS)

    Problem Description Xiao Ming is traveling around several cities by train. And the time on the train ...

  7. [计算机故障]为什么我的手机SD卡一打开就是说“你的磁盘未格式化,现在需要格式化吗”?

    现在随着智能手机的普及,越来越多的人使用到了手机SD卡.也有的是micro SD(更小一些). 最近一个朋友说,为什么我的手机SD卡插到手机里一打开就是说“你的磁盘未格式化,现在需要格式化吗?” 但是 ...

  8. Hihocoder #1479 : 三等分 树形DP

    三等分  描述 小Hi最近参加了一场比赛,这场比赛中小Hi被要求将一棵树拆成3份,使得每一份中所有节点的权值和相等. 比赛结束后,小Hi发现虽然大家得到的树几乎一模一样,但是每个人的方法都有所不同.于 ...

  9. 1.jdk安装和环境配置

    这个简单,但是记不住,非要网上搜一遍不可: 1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值:C:\Progr ...

  10. linux switch_root

    1 命令格式 switch_root newroot init 跳转到另外一个文件系统,并且把newroot作为新的mount tree,并且执行init程序. 2 特殊要求 newroot必须是一个 ...