482 C. Game with Strings

题意:给定n个长度为m的串(n<=50,m<=20),保证每个串互不相同.每次可以问目标串一个位置上的字母,问最终猜出目标串的期望次数.
解法:状态压缩dp.
Let dp[mask] be the expected number of questions from state mask. When we try to ask question pos in state mask, the chance that we will not guess correctly right away is equal to the fraction frac = bitCount(d[mask ^ (1<<pos)]) / bitCount(d[mask]).
So dp[mask] is equal to the average of 1 + dp[mask ^ (1<<pos)] * frac, for all pos not in mask.
#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <iomanip>
using namespace std;

string s[55];
long long bad[1 << 20];
double  dp[1 << 20];
int n, m;

int main()
{
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(16);
    cin >> n;
    for (int i = 0; i < n; ++ i) {
        cin >> s[i];
    }
    m = (int)s[0].length();
    for (int i = 0; i < n; ++ i) {
        for (int j = i + 1; j < n; ++ j) {
            int diff = 0;
            for (int k = 0; k < m; ++ k) {
                if (s[i][k] == s[j][k]) {
                    diff |= 1 << k;
                }
            }
            bad[diff] |= (1ll << i) + (1ll << j);
        }
    }
    for (int i = (1 << m) - 2; i >= 0; -- i) {
        for (int j = 0; j < m; ++ j) {
            if (((i >> j) & 1) == 0) {
                bad[i] |= bad[(i | (1 << j))];
            }
        }
        if (bad[i] == 0) {
            continue;
        }
        int c = m - __builtin_popcount(i);
        int d = __builtin_popcountll(bad[i]);
        for (int j = 0; j < m; ++ j) {
            if (((i >> j) & 1) == 0) {
                dp[i] += dp[i | 1 << j] * __builtin_popcountll(bad[i | 1 << j]) / d;
            }
        }
        dp[i] = 1. + dp[i] / c;
    }
    cout<<dp[0]<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值