Codeforces 482C Game with Strings(dp+概率)

本文介绍了一道 Codeforces 平台上的字符串游戏问题的解题思路与实现代码。该题要求通过概率性的询问确定一个未知的字符串,文章详细解析了解决方案,并给出了一种高效的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值