F - Dreamoon and WiFi 组合数学
来源:互联网 发布:哈佛大学知乎 编辑:程序博客网 时间:2024/05/22 12:02
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:
- Go 1 unit towards the positive direction, denoted as '+'
- 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
++-+-+-+-+
1.000000000000
+-+-+-??
0.500000000000
+++??-
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)));
}
}
}
- F - Dreamoon and WiFi 组合数学
- codeforces B. Dreamoon and WiFi 组合数学求概率
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp,组合数学
- CodeForces 476B Dreamoon and WiFi 数学 概率 DP
- B. Dreamoon and WiFi
- Dreamoon and WiFi
- Dreamoon and WiFi
- Dreamoon and Sums 数学
- 【CODEFORCES】 B. Dreamoon and WiFi
- G - Dreamoon and Sums 数学
- CF 272B Dreamoon and WiFi
- B. Dreamoon and WiFi(Codeforces Round 272)
- Codeforces 476B. Dreamoon and WiFi
- Codeforces 476B Dreamoon and WiFi
- Dreamoon and WiFi CodeForces - 476B
- CF 476C Dreamoon and Sums[数学]
- Dreamoon and WiFi ???下同一位置的概率
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi 暴力
- Java设计模式之工厂模式
- qmake的介绍
- 适配swift3
- 论文笔记《A CNN Regression Approach for Real-Time 2D/3D Registration 》
- unix的grep命令
- F - Dreamoon and WiFi 组合数学
- ununtu 14.04 安卓7.0 编译
- iOS App组件化开发实践
- C++並發 練習筆記(二)使用boost Asio的async I/O技術實作簡易網路聊天室 (下)
- rac重启遭遇ORA-01078、ORA-01565、ORA-17503、ORA-12547
- Spark之中map与flatMap的区别
- qmake之qmake的安装
- Hello World!
- Servlet学习之六cookie的用法