Description
给出一个移动序列,+表示往正方向移动一单位,
Input
两个长度不超过
Output
输出两个序列终点相同的概率
Sample Input
+-+-
+-??
Sample Output
0.500000000000
Solution
设第一个移动序列的终点为pos,第二个移动序列中+有
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
char a[maxn],b[maxn];
double C(int n,int m)
{
double ans=1;
for(int i=1;i<=m;i++)ans=ans*(n-m+i)/i;
return ans;
}
int main()
{
while(~scanf("%s%s",a,b))
{
int len=strlen(a);
int pos=0;
for(int i=0;i<len;i++)
if(a[i]=='+')pos++;
else pos--;
int n1=0,n2=0,n3=0;
for(int i=0;i<len;i++)
if(b[i]=='+')n1++;
else if(b[i]=='-')n2++;
else n3++;
int x=pos+n2-n1+n3,y=n3+n1-n2-pos;
if(x<0||y<0||x&1||y&1)printf("0\n");
else
{
x/=2,y/=2;
printf("%.10f\n",C(n3,x)/(1<<n3));
}
}
return 0;
}