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 题目大意:给你一颗树,每个节点都会长苹果,然后每一秒钟,苹果往下滚一个.两个两个会抵消苹果.问最后在根 ...
随机推荐
- maven依赖scope配置项讲解
我们在使用Maven配置依赖项的时候,常常只会配置Maven的坐标以及版本信息就可以了,但我们看其他人的工程代码的时候常常会见到有个scope配置项,今天就来分别介绍下这个配置下几个类别的作用. &l ...
- java线程游戏之背景图片的移动
package com.plane; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; im ...
- js实现导航栏的吸顶操作
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- linq to sql 不能更新的问题
今天在项目中用linq更新一个表的时候,结果怎么都更新不了,最蛋疼的是什么异常也不报,发现db.table1.isReadOnly为True 知道问题所在,百度后得到解决办法: 原来是我的表没有增加主 ...
- iOS 通过接受距离传感器的消息改变屏幕的明暗度(仅限用于真实的手机)
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL ...
- 一个小错误,在for循环中,每次repaint()的时候,记得先把frame涂成白色的。等于擦掉原来的痕迹·。
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Animate { int x=1; in ...
- Luogu 4409 [ZJOI2006]皇帝的烦恼
BZOJ 1863 lyd口中的夹B递推. 挺妙的解法. 第一个感觉是找到一个最大的相邻的$a_i + a_{i - 1}$就可以了,但是这个想法大概只对了一半,一半的意思是说只有在$n$为偶数的时候 ...
- 3.文档视图:从gui分割状态
为了解决一个类实现所有功能的缺陷,我们把application分为2个部分.一个部分业务逻辑,一个部分视觉渲染和交互.这2个类在学术上被称为document view 或者 model delegat ...
- html相关标记的含义
HTML标记含义1.<html>...</html> :html 文档标记2.<head>...</head> :文档头标记3.<title> ...
- 《Head First Servlets & JSP》-6-会话管理-listener etc. demo
工程结构 上下文参数示例 示例程序展示了如何从上下文读取参数,并在上下文监听器中生成属性对象和在上下文中设置属性. 建立一个简单的JavaBean对象作为属性:Dog.java package com ...