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 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
++-+-
+-+-+
Sample Output
1.000000000000
Hint
题意
给你一个串s1,和一个串s2
然后s2中有一些问号,有0.5的概率是向左边,有0.5的概率是向右边走。
问你s2结束之后,恰好走到s1串走到的目的地的概率是多少。
题解:
数据范围很小,怎么做都行。
我是概率dp,dp[i][j]表示考虑到执行第二个串的第i位现在在j位置的概率是多少。
代码
#include<bits/stdc++.h>
using namespace std;
double dp[100][100];
char s1[50],s2[50];
int main()
{
scanf("%s%s",s1+1,s2+1);
int level = 50;
int pos=50;
int len1 = strlen(s1+1);
for(int i=1;i<=len1;i++)
{
if(s1[i]=='+')pos++;
else pos--;
}
dp[0][level]=1;
for(int i=1;i<=len1;i++)
{
if(s2[i]=='+')for(int j=1;j<100;j++)
dp[i][j]=dp[i-1][j-1];
if(s2[i]=='-')for(int j=0;j<99;j++)
dp[i][j]=dp[i-1][j+1];
if(s2[i]=='?')for(int j=1;j<99;j++)
dp[i][j]=0.5*dp[i-1][j-1]+0.5*dp[i-1][j+1];
}
printf("%.12f\n",dp[len1][pos]);
}
Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp的更多相关文章
- 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) E. Dreamoon and Strings dp
题目链接: http://www.codeforces.com/contest/476/problem/E E. Dreamoon and Strings time limit per test 1 ...
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi (暴力二进制枚举)
题意:给你一个只含\(+\)和\(-\)的字符串,统计它的加减和,然后再给你一个包含\(+,-,?\)的字符串,其中\(?\)可以表示为\(+\)或\(-\),问有多少种情况使得第二个字符串的加减和等 ...
- Codeforces Round #272 (Div. 2) E. Dreamoon and Strings 动态规划
E. Dreamoon and Strings 题目连接: http://www.codeforces.com/contest/476/problem/E Description Dreamoon h ...
- Codeforces Round #272 (Div. 2) D. Dreamoon and Sets 构造
D. Dreamoon and Sets 题目连接: http://www.codeforces.com/contest/476/problem/D Description Dreamoon like ...
- Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs 水题
A. Dreamoon and Stairs 题目连接: http://www.codeforces.com/contest/476/problem/A Description Dreamoon wa ...
- Codeforces Round #272 (Div. 1) A. Dreamoon and Sums(数论)
题目链接 Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occa ...
- Codeforces Round #272 (Div. 2)-C. Dreamoon and Sums
http://codeforces.com/contest/476/problem/C C. Dreamoon and Sums time limit per test 1.5 seconds mem ...
- Codeforces Round #272 (Div. 2)-A. Dreamoon and Stairs
http://codeforces.com/contest/476/problem/A A. Dreamoon and Stairs time limit per test 1 second memo ...
随机推荐
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence
http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...
- Linux命令(五)免密码远程登录和配置别名
1. ssh-keygen 2. ssh-copy-id -p port user@remote .ssh中建立并编辑config文件 原来需要 ssh -p ubuntu@xxx.xxx.xxx 现 ...
- Python 入门基础9 --函数基础2 实参与形参
今日内容: 一.函数参数 1.形参与实参定义 2.实参分类 3.形参分类 4.可变参数的整体使用 一.形参与实参定义 def fn(参数们): pass 1.1 形参 定义函数,在括号内声明的变量名, ...
- 云计算-MapReduce
Hadoop示例程序WordCount详解及实例http://blog.csdn.net/xw13106209/article/details/6116323 hadoop中使用MapReduce编程 ...
- 【API】网络编程模型、多线程
1.网络通信编程 1)网络通信模型基础知识 TCP Server: WSAStartup() socket() bind() linsten() accept() send/recv() closes ...
- 理解 Memory barrier(内存屏障)【转】
转自:http://name5566.com/4535.html 参考文献列表:http://en.wikipedia.org/wiki/Memory_barrierhttp://en.wikiped ...
- Casperjs循环执行(重复执行不退出)
var casper = require('casper').create({ // pageSettings: { // loadImages: true, // loadPlugins: fals ...
- 关于Spring 事务管理传播属性的配置及作用-嵌套事务
先了解事务的7种传播属性: PROPAGATION_REQUIRED -- 支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. PROPAGATION_SUPPORTS -- 支持当前 ...
- oracle列转行
unpivot()函数需要Oracle版本大于等于11g --创建表 create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 in ...
- AngularJS里面$emit, $broadcast,$on,$http.Jsonp,constant是使用笔记
本片主要介绍$emit, $broadcast,$on经常开发的用法!