F - Dreamoon and WiFi 组合数学

来源:互联网 发布:哈佛大学知乎 编辑:程序博客网 时间:2024/05/22 12:02
F - Dreamoon and WiFi
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 476B

Description

Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.

Each command is one of the following two types:

  1. Go 1 unit towards the positive direction, denoted as '+'
  2. Go 1 unit towards the negative direction, denoted as '-'

But the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5).

You are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?

Input

The first line contains a string s1 — the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+''-'}.

The second line contains a string s2 — the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+''-''?'}. '?' denotes an unrecognized command.

Lengths of two strings are equal and do not exceed 10.

Output

Output a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.

Sample Input

Input
++-+-+-+-+
Output
1.000000000000
Input
+-+-+-??
Output
0.500000000000
Input
+++??-
Output
0.000000000000

Hint




#include<cstring>
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
char s[111],s1[111];
int C(int num2,int x)//计算组合数;
{
    int sum=1,sum1=1;
     for(int i=1;i<=num2;i++)
     sum*=i;
    for(int i=1;i<=x;i++)
    sum1*=i;
    int sum2=1;
    for(int i=1;i<=num2-x;i++)
         sum2*=i;
    return sum/(sum2*sum1);
}
int main()
{
   scanf("%s\n%s",s,s1);
   int sum1=0;
   int l1=strlen(s);
  // int l2=strlen(s1);
   for(int i=0;i<l1;i++)
     if(s[i]=='+')
       sum1++;
     else
     sum1--;
     int sum2=0;
     int num2=0;
     for(int i=0;i<l1;i++)
     {
      if(s1[i]=='+')
          sum2++;
      else if(s1[i]=='-')
          sum2--;
      else
        num2++;
     }
    if(sum1==sum2&&num2==0)
     printf("%.12lf\n",1.0);
    else if(sum1!=sum2&&num2==0)
     printf("%.12lf\n",0.0);
    else
    {
      int d=abs(sum2-sum1);
      if((num2+d)&1)
      printf("%.12lf\n",0.0);
     else
     {
       int x1=(abs(sum2-sum1)+num2)/2;
       int aa=C(num2,x1);
       printf("%.12lf\n",aa*(double)(pow(0.5,x1))*(double)(pow(0.5,num2-x1)));
     }
    }
}



0 0