Dreamoon and WiFi ???下同一位置的概率

来源:互联网 发布:mac怎么设置屏幕 编辑:程序博客网 时间:2024/05/16 18:39
B. Dreamoon and WiFi
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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:

  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 exceed 10 - 9.

题目意思:http://codeforces.com/contest/476/problem/B

初始在0的位置,+往右移动1,-往左移动1,一个串s之后会停止某个位置。另外,输入一个串str,存在?,问最后在同一个位置的概率是多少。

dfs把2^cnt全部搜出来,暴力计数就行了。

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<vector>#include<queue>#include<map>#include<stack>#define rt return#define bk break#define ct continue#define sf scanf#define pf printf#define ms memset#define si(n) sf("%d",&n)#define pi(n) pf("%d\n",n)#define REP0(i,n) for(int i=0;i<(n);i++)#define REP1(i,n) for(int i=1;i<=(n);i++)#define REP(i,s,n) for(int i=s;i<=(n);i++)#define db double#define op operator#define pb push_back#define LL long long#define INF 0x3fffffff#define eps 1e-8#define PI acos(-1)#define maxn 1010using namespace std;int res;void dfs(char s[],int cnt,int ans1,int ans2){    if(cnt==ans1+ans2){        int tmp1=0,tmp2=0;        for(int i=0;i<cnt;i++)            if(s[i]=='+')tmp1++;            else tmp2++;        if(tmp1==ans1&&tmp2==ans2)            res++;        rt ;    }    s[cnt]='+';    dfs(s,cnt+1,ans1,ans2);    s[cnt]='-';    dfs(s,cnt+1,ans1,ans2);}int main(){    #ifdef ACBang    freopen("in.txt","r",stdin);    #endif    char s1[20],s2[20];    while(~sf("%s%s",s1,s2)){        int cnt1=0,cnt2=0;        int ans1=0,ans2=0,ans=0;        int len=strlen(s1);        for(int i=0;i<len;i++){            if(s1[i]=='+')cnt1++;            else cnt2++;            if(s2[i]=='+')ans1++;            else if(s2[i]=='-')ans2++;            else ans++;        }        //if(cnt1<ans1||cnt2<ans2)puts("0.000000000000");        //else{            ans1=cnt1-ans1;            ans2=cnt2-ans2;            res=0;            char s[20];            dfs(s,0,ans1,ans2);            pf("%.12lf\n",res*1.0/pow(2.0,1.0*(ans1+ans2)));        //}    }    rt 0;}


0 0
原创粉丝点击