B. Dreamoon and WiFi(Codeforces Round 272)
1 second
256 megabytes
standard input
standard output
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?
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 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.
++-+-
+-+-+
1.000000000000
+-+-
+-??
0.500000000000
+++
??-
0.000000000000
For the first sample, both s1 and s2 will
lead Dreamoon to finish at the same position + 1.
For the second sample, s1 will
lead Dreamoon to finish at position 0, while there are four possibilites for s2:
{"+-++", "+-+-", "+--+","+---"}
with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4,
so the probability of finishing at the correct position is 0.5.
For the third sample, s2 could
only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position + 3 is 0.
将全部可能的路径dfs推断一遍即可了。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s1[20],s2[20];
int acou=0,dao=0,sum=0;
int n1,n2;
void solve(int n,int at)
{
if(n==n2)
{
if(at==acou)
dao++;
sum++;
return;
}
if(s2[n]=='+')
solve(n+1,at+1);
else if(s2[n]=='-')
solve(n+1,at-1);
else
{
solve(n+1,at+1);
solve(n+1,at-1);
}
} int main()
{
scanf("%s",s1);
scanf("%s",s2);
n1=strlen(s1);
n2=strlen(s2);
for(int i=0;i<n1;i++)
if(s1[i]=='+')
acou++;
else
acou--;
solve(0,0);
printf("%.12f\n",(double)(dao)/(sum));
return 0;
}
B. Dreamoon and WiFi(Codeforces Round 272)的更多相关文章
- A. Dreamoon and Stairs(Codeforces Round #272)
A. Dreamoon and Stairs time limit per test 1 second memory limit per test 256 megabytes input standa ...
- E. Dreamoon and Strings(Codeforces Round #272)
E. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...
- D. Dreamoon and Sets(Codeforces Round #272)
D. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #272 (Div. 2) 题解
Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs time limit per test 1 second memory limit per ...
- Codeforces Round #272 (Div. 2)-B. Dreamoon and WiFi
http://codeforces.com/contest/476/problem/B B. Dreamoon and WiFi time limit per test 1 second memory ...
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp
B. Dreamoon and WiFi 题目连接: http://www.codeforces.com/contest/476/problem/B Description Dreamoon is s ...
- Codeforces Round #272 (Div. 2) Dreamoon and WiFi 暴力
B. Dreamoon and WiFi Dreamoon is standing at the position 0 on a number line. Drazil is sending a li ...
- Codeforces Round #272 (Div. 2)
A. Dreamoon and Stairs 题意:给出n层楼梯,m,一次能够上1层或者2层楼梯,问在所有的上楼需要的步数中是否存在m的倍数 找出范围,即为最大步数为n(一次上一级),最小步数为n/2 ...
- Codeforces Round #272 (Div. 2)AK报告
A. Dreamoon and Stairs time limit per test 1 second memory limit per test 256 megabytes input standa ...
随机推荐
- 怎样在Android本地视频播放器开发
在上一章Android本地视频播放器开发--SDL编译编译中编译出sdl的支持库,当时我们使用的2.0,但是有些api被更改了,所以在以下的使用者中我们使用SDL1.3的库,这个库我会传上源码以及编译 ...
- Android在onInterceptTouchEvent与onTouchEvent
onInterceptTouchEvent: onInterceptTouchEvent是在ViewGroup里面定义的.Android中的layout布局类一般都是继承此类的.onIntercept ...
- Android中canvas.save()和canvas.restore()的使用
自己定义控件时经常遇到重写View的Ondraw()方法,Ondraw()方法经常设计到save()和restore()这两个方法.这两个相互匹配出现的,作用是用来保存画布的状态和取出保存的状态的. ...
- java特权制度设计篇
数据库表设计 1.1.1. 模块管理(jk_module) 项 类型 说明 id long 就是数据库记录idx号 name string 模块名称 orderid int 排序id 由小到大显 ...
- POJ 1300 Door Man - from lanshui_Yang
Description You are a butler in a large mansion. This mansion has so many rooms that they are merely ...
- linux LVS DR模式配置
拓扑图: 测试环境:CentOS 6.5 X86 64位 配置步骤: 1. 安装测试环境 [root@UCS-1 ~]# yum -y install httpd [root@UCS-1 ~]# c ...
- redhat linux使用Centos yum源
redhat Linux如果是没有购买红帽许可的话是不能使用redhat的yum源的,但是可以通过修改使之能使用Centos的yum源. 步骤一:删除redhat的yum [root@localhos ...
- android关于实现滑动界面
首先要说的是,滑动界面,我们需要一个以上的view切换,实际上可以使用ArrayList<View> pageViews要保存view信息,然后切换 LayoutInflater infl ...
- 调用微信退款接口时,证书验证出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法
1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应用程序池-->右击-->高级设置- ...
- Max Sum (hdu 1003 简单DP水过)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...