codeforces 482c Game with Strings

http://www.elijahqi.win/archives/1311
You play the game with your friend. The description of this game is listed below.

Your friend creates n distinct strings of the same length m and tells you all the strings. Then he randomly chooses one of them. He chooses strings equiprobably, i.e. the probability of choosing each of the n strings equals . You want to guess which string was chosen by your friend.

In order to guess what string your friend has chosen, you are allowed to ask him questions. Each question has the following form: «What character stands on position pos in the string you have chosen?» A string is considered guessed when the answers to the given questions uniquely identify the string. After the string is guessed, you stop asking questions.

You do not have a particular strategy, so as each question you equiprobably ask about a position that hasn’t been yet mentioned. Your task is to determine the expected number of questions needed to guess the string chosen by your friend.
Input

The first line contains a single integer n (1 ≤ n ≤ 50) — the number of strings your friend came up with.

The next n lines contain the strings that your friend has created. It is guaranteed that all the strings are distinct and only consist of large and small English letters. Besides, the lengths of all strings are the same and are between 1 to 20 inclusive.
Output

Print the single number — the expected value. Your answer will be considered correct if its absolute or relative error doesn’t exceed 10 - 9.
Examples
Input

2
aab
aac

Output

2.000000000000000

Input

3
aaA
aBa
Caa

Output

1.666666666666667

Input

3
aca
vac
wqq

Output

1.000000000000000

Note

In the first sample the strings only differ in the character in the third position. So only the following situations are possible:

you guess the string in one question. The event's probability is ;
you guess the string in two questions. The event's probability is · = (as in this case the first question should ask about the position that is other than the third one);
you guess the string in three questions. The event's probability is ·· = ;

Thus, the expected value is equal to

In the second sample we need at most two questions as any pair of questions uniquely identifies the string. So the expected number of questions is .

In the third sample whatever position we ask about in the first question, we immediately identify the string.

我们设dp[s]表示在s状态下我有多少的可能不能够得到确切的答案 s中一表示我已经查过

首先预处理c数组 表示我已经询问了s状态的列 然后有多少个数组我已经区分开了

可以怎么求 我们可以先求相反的 设立d数组 表示 询问了s状态的列 但是我还有哪些位置无法区分

那么若一个状态无法区分 那么它的子集也一定无法区分

相当于每个都可以识别的状态都先打上标记 然后最后再下传标记即可

在算概率的时候 我新生成的状态tmp1=dp[s]/1-step 表示 在剩余没选的列中再选一列的概率是多少

然后 乘以我这次询问不可以确定的串的数量(c[next]-c[s])/not_done 就可以得出我下一个状态无法得到的概率 为了方便统计 把其加入p数组 最后统计出来 一起计算一下期望输出就好

#include<cstdio>
#include<cstring>
#define S (1<<20)+10
#define N1 55
#define ll long long 
long long d[S],bin[N1];
char str1[N1][N1];
double p[22],dp[S];int n,c[S];
inline int calc(ll s){
    int cnt=0;
    while (s){if (s&1) cnt++;s>>=1;}return cnt;
}
int main(){
    freopen("cf.in","r",stdin);
    scanf("%d",&n);if (n==1){printf("0");return 0;} 
    for (int i=0;i<n;++i) scanf("%s",str1[i]);
    int l=strlen(str1[0]);for (int i=0;i<55;++i) bin[i]=1LL<<i;
    //d表示询问这些列 有那些字符串是无法区分的 
    for (int i=0;i<n;++i)
        for (int j=i+1;j<n;++j){
            int s=0;
            for (int z=0;z<l;++z)
                if (str1[i][z]==str1[j][z]) s|=bin[z];
            d[s]|=bin[i]|bin[j];
        }
    int t=1<<l;
    for (int i=t-1;i;--i){
        for (int j=0;j<l;++j) if (i&bin[j]) d[i^bin[j]]|=d[i];
        c[i]=n-calc(d[i]);
    }dp[0]=1;
    for (int s=0;s<=bin[l]-1;++s){
        double not_done=n-c[s];int step=calc(s);double tmp1=dp[s]/(l-step);
        if (c[s]==n) continue;
        for (int i=0;i<l;++i){
            if (!(s&bin[i])){
                int next=s|bin[i];double now=c[next]-c[s];
                double tmp=now/not_done;dp[next]+=tmp1*(1-tmp);
                p[step+1]+=tmp*tmp1;
            }
        }
    }double ans=0;
    for (int i=1;i<=l;++i)ans+=p[i]*i;
    printf("%.9lf",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值