CF#272 (Div. 2) B.(dfs)

来源:互联网 发布:linux 播放器剪辑 编辑:程序博客网 时间:2024/05/01 13:03
B. Dreamoon and WiFi
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
题目链接:http://codeforces.com/contest/476/problem/B

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:

  1. Go 1 unit towards the positive direction, denoted as '+'
  2. 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 exceed10 - 9.

Sample test(s)
Input
++-+-+-+-+
Output
1.000000000000
Input
+-+-+-??
Output
0.500000000000
Input
+++??-
Output
0.000000000000
Note

For the first sample, both s1 ands2 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 fors2: {"+-++","+-+-", "+--+","+---"} with ending position {+2, 0, 0, -2} respectively. So there are2 correct cases out of 4, so the probability of finishing at the correct position is0.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.




解题思路:

        题意就是给你两个串,每个串最开始的值默认为0,' +' 代表+1,' - ' 代表-1。第一个串是完整给出的,第二个串可能会出现' ? ',代表此处可以+1,也可以-1,问对于第二个串来说,它与第一个串最后值相等的概率是多少。

        首先很容易发现,如果第二个串有n个问号,那对应的情况种数是2^n。那接下来我们的任务就是求出和第一个串最后值相等的情况有多少种就行了。kk姐姐当初是用数学方法做的,我第一眼就想用dfs·····因为最近在做搜索·····,被姐姐深深地鄙视了······

        dfs搜索每种状态(+1和-1),如果连个串最后的值相等,那么cnt++。最后除法时强制类型转换下,注意下输出位数就OK了。



完整代码:

#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;int cnt;int ans;void dfs(int pro , int sum){    if(pro == 0)    {        if(sum == ans)            cnt ++;        return;    }    else    {            dfs(pro - 1 , sum + 1);        dfs(pro - 1 , sum - 1);    }}int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    string s , t;    while(cin >> s >> t)    {        int len = s.length();        int value1 = 0 , value2 = 0;        for(int i = 0 ; i < len ; i ++)        {            if(s[i] == '+')                value1 += 1;            if(s[i] == '-')                value1 -= 1;        }        int pro = 0;        for(int i = 0 ; i < len ; i ++)        {            if(t[i] == '+')                value2 += 1;            if(t[i] == '-')                value2 -= 1;            if(t[i] == '?')                pro ++;        }        ans = value1 - value2;        int res = 1;        for(int i = 0 ; i < pro ; i ++)            res *= 2;        cnt = 0;        dfs(pro , 0);        printf("%.12lf\n" , (double)cnt / (double)res);    }}


0 0