CodeForces - 476B -Dreamoon and WiFi(DFS+概率思维)
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.
Examples
Input
++-+-
+-+-+
Output
1.000000000000
Input
+-+-
+-??
Output
0.500000000000
Input
+++
??-
Output
0.000000000000
Note
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暴搜,然后去比较是否相等和sum1相等,相等s就加1,每次有问号会产生2的问号个数次方的情况,最后进行概率的转化即可
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int sum1=0;
int sum2=0;
int sum3=0;
int s=0;
void dfs(int s1,int x)
{
if(s1==0)
{
if(x==sum1)
s++;
return ;
}
dfs(s1-1,x+1);
dfs(s1-1,x-1);
}
int main()
{
string str1,str2;
cin>>str1>>str2;
for(int t=0;t<str1.length();t++)
{
if(str1[t]=='+')
{
sum1+=1;
}
if(str1[t]=='-')
{
sum1--;
}
}
for(int t=0;t<str2.length();t++)
{
if(str2[t]=='+')
{
sum2++;
}
if(str2[t]=='-')
{
sum2--;
}
if(str2[t]=='?')
{
sum3++;
}
}
int k=sum3;
if(sum3==0)
{
if(sum2==sum1)
{
printf("1.000000000000\n");
}
else
{
printf("0.000000000000\n");
}
}
else
{
dfs(sum3,sum2);
double ss=s*1.0/pow(2,k);
printf("%.12f\n",ss);
}
return 0;
}
CodeForces - 476B -Dreamoon and WiFi(DFS+概率思维)的更多相关文章
- codeforces 476B.Dreamoon and WiFi 解题报告
题目链接:http://codeforces.com/problemset/problem/476/B 题目意思:给出两个字符串str1, str2,其中,str1 只由 '+' 和 '-' 组成,而 ...
- 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)-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) Dreamoon and WiFi 暴力
B. Dreamoon and WiFi Dreamoon is standing at the position 0 on a number line. Drazil is sending a li ...
- B. Dreamoon and WiFi(Codeforces Round 272)
B. Dreamoon and WiFi time limit per test 1 second memory limit per test 256 megabytes input standard ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)
题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...
- Codeforces 931D Peculiar apple-tree(dfs+思维)
题目链接:http://codeforces.com/contest/931/problem/D 题目大意:给你一颗树,每个节点都会长苹果,然后每一秒钟,苹果往下滚一个.两个两个会抵消苹果.问最后在根 ...
随机推荐
- Linux 2.6 中的文件锁
简介: 本文的目的是想帮助读者理清 Linux 2.6中文件锁的概念以及 Linux 2.6 都提供了何种数据结构以及关键的系统调用来实现文件锁,从而可以帮助读者更好地使用文件锁来解决多个进程读取同一 ...
- Android ListView 设置
android:minHeight="80dip"//设置每一条的高度 android:divider="@null" //设置默认的分割线不显示 androi ...
- webform 内置对象(页面间传值)
QueryString/URL传值 页面名后面加?变量名=值 有点:不占服务器内存. 缺点:保密性差:传递字符串长度有限. Response --相应请求对象 Response.Redirect ...
- mac上virtualBox的安装和使用
一.下载和安装 去oracle官网下载mac版的virtualBox. 官网下载地址:https://www.virtualbox.org/. 下载好后按照向导进行安装即可. 二.使用方法 1.新建虚 ...
- HDOJ 1164 Eddy's research I
Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Ed ...
- vray学习笔记(5)-学习资料
首先肯定是vray的官方的资料了: 一个是教程 https://docs.chaosgroup.com/display/VRAY3MAX/Tutorials 一个是帮助文件 https://docs. ...
- R: plot 绘图各种参数,以及 legend。。
################################################### 问题:基础绘图plot 18.4.30 plot函数,基础绘图的各个参数? 解决方案: ca ...
- 715B Complete The Graph
传送门 题目大意 给出一个图,一些边带权,另一些边等待你赋权(最小赋为1).请你找到一种赋权方式,使得 s 到 t 的最短路为 L n ≤ 1e3 ,m ≤ 1e4 ,L ≤ 1e9 分析 二分所有边 ...
- redis过期时间设置
方法一: $redis->setex(,'huahua'); 方法二: $redis->set('name','huahua'); $redis->expire('name',3);
- jqgrid列动态加载
private void InitGrid(string entityName) { Session["entityName"] = entityName; ArrayList c ...