码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 482C. Game with Strings 状压DP

时间:2015-03-09 12:51:53      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:


很好的状压dp题目

d[mask]=x   在询问了mask位的情况下,有x状态的串还是不能区分 /// 预处理不好会TLE

dp[x]  从一次也没有问,到问到状态x时的概率


可以看 http://blog.csdn.net/houserabbit/article/details/40658791 大神的题解

C. Game with Strings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
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.



/* ***********************************************
Author        :CKboss
Created Time  :2015年03月08日 星期日 21时18分00秒
File Name     :CF482C_C.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;

char str[55][22];

int n,m;
LL d[(1<<20)+10];
double dp[(1<<20)+10];

int countb(LL x)
{
	int ans=0;
	while(x) { ans++; x=x&(x-1LL); }
	return ans;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%s",str[i]);
	m=strlen(str[0]);

	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(j==i) continue;
			/// string i and string j has public pos
			int same=0;
			for(int k=0;k<m;k++)
			{
				if(str[i][k]==str[j][k]) same|=(1<<k);
			}
			d[same]|=(1LL<<i); d[same]|=(1LL<<j); 
		}
	}
	// d[mask] <= d[mask^(1<<i)]
	for(int mask=(1<<m)-1;mask;mask--)
	{
		for(int j=0;j<m;j++)
		{
			if((mask>>j)&1)
			{
				int nmask=mask^(1<<j);
				d[nmask]|=d[mask];
			}
		}
	}

	dp[0]=1;
	double ans=0;
	for(int mask=0;mask<(1<<m);mask++)
	{
		int c=countb(mask);
		for(int j=0;j<m;j++)
		{
			if(((mask>>j)&1)==0)
			{
				int nmask=mask|(1<<j);
				dp[nmask]+=dp[mask]/(m-c);
			}
		}
		for(int j=0;j<n;j++)
		{
			if((d[mask]>>j)&1LL) ans+=dp[mask];
		}
	}

	printf("%.15lf\n",ans/n);

    return 0;
}



Codeforces 482C. Game with Strings 状压DP

标签:

原文地址:http://blog.csdn.net/ck_boss/article/details/44153157

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!