codeforces 476 B. Dreamoon and WiFi
http://codeforces.com/problemset/problem/476/B 题意:给出两个长度相等-且不超过10的字符串,串1只包含‘-’,’+‘。按照‘+’为1,‘-’为-1累加可以得到一个值。串2还包含若干‘?’,代表该处的值不确定,且为’+‘和’-‘的概率相等,都是0.5.问串2的值和串1相等的概率。 思路:我们可以扫一遍得到‘?’的个数和两个式子的差值。设问号个数为a,差值为b,那么在a个问号中需要有(a-b)/2个为‘+’(容易知道,a,b一定奇偶性相同,所以a-b一定能被2整除),根据超几何分布,概率为 c[a][(a-b)/2]*(1/2)^a; 写的时候可以先打个组合数的表。1A,开心。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年02月02日 星期二 03时32分39秒
4File Name :code/cf/problem/476B.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33string s1,s2;
34int len;
35int c[20][20];
36void pre()
37{
38 ms(c,0);
39 c[1][1] = 1;
40 c[1][2] = 1;
41 c[2][1] = 1;
42 c[2][2] = 2;
43 c[2][3] = 1;
44 for ( int i =3 ; i <=15 ; i++)
45 for ( int j = 0 ; j <= i ; j++)
46 c[i][j+1] = c[i-1][j+1]+c[i-1][j];
47}
48int main()
49{
50 #ifndef ONLINE_JUDGE
51 freopen("code/in.txt","r",stdin);
52 #endif
53 pre();
54
55 cin>>s1>>s2;
56 len = s1.length();
57 int a=0,b=0;
58 for ( int i = 0 ; i < len ; i++)
59 {
60 if (s2[i]=='?') a++;
61 if (s1[i]=='+') b++;
62 else b--;
63 if (s2[i]=='+') b--;
64 else if (s2[i]=='-') b++;
65 }
66// cout<<"a:"<<a<<endl;
67// cout<<"b:"<<b<<endl;
68 if (a==0)
69 {
70 if (b==0) puts("1");
71 else puts("0");
72 }
73 else
74 {
75 double ans = c[a][(a-b)/2+1]*1.0;
76 for ( int i = 1 ; i <= a ; i ++) ans *=0.5;
77 printf("%.11lf\n",ans);
78 }
79
80 #ifndef ONLINE_JUDGE
81 fclose(stdin);
82 #endif
83 return 0;
84}