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,开心。

/* ***********************************************
Author :111qqz
Created Time :2016年02月02日 星期二 03时32分39秒
File Name :code/cf/problem/476B.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6string s1,s2;
 7int len;
 8int c[20][20];
 9void pre()
10{
11    ms(c,0);
12    c[1][1] = 1;
13    c[1][2] = 1;
14    c[2][1] = 1;
15    c[2][2] = 2;
16    c[2][3] = 1;
17    for ( int i =3 ; i  <=15 ; i++)
18	for ( int j = 0 ; j <= i ; j++)
19	    c[i][j+1] = c[i-1][j+1]+c[i-1][j];
20}
21int main()
22{
23	#ifndef  ONLINE_JUDGE 
24	freopen("code/in.txt","r",stdin);
25  #endif
26	pre();
 1	cin>>s1>>s2;
 2	len = s1.length();
 3	int a=0,b=0;
 4	for ( int i = 0 ; i < len ; i++)
 5	{
 6	    if (s2[i]=='?') a++;
 7	    if (s1[i]=='+') b++;
 8		else b--;
 9	    if (s2[i]=='+') b--;
10		else if (s2[i]=='-') b++;
11	}
12//	cout<<"a:"<<a<<endl;
13//	cout<<"b:"<<b<<endl;
14	if (a==0)
15	{
16	    if (b==0) puts("1");
17	    else puts("0");
18	}
19	else
20	{
21	    double ans = c[a][(a-b)/2+1]*1.0;
22	    for ( int i = 1 ; i <= a ; i ++) ans *=0.5;
23	    printf("%.11lf\n",ans);
24	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}