CodeForce-476B-Dreamoon and WiFi

版权声明:欢迎评论与转载,转载时请注明出处! https://blog.csdn.net/wjl_zyl_1314/article/details/83478184

原题
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.
题意:
有两个字符串s1,s2。s1由“+”“-”组成,s2由“+”“-”“?”组成。“+”代表向正方向走一步,“-”代表向反方向走一步,“?”代表想做或者向右走一步,且可能性相同为0.5.现在小明在数轴上的0处,他原本应该按照s1走到X处,现在却由于WIFI不好,只能按照s2走,问也能走到X处的可能性。结果保留12位小数。
题解:
也是模拟水题,只需要先计算出正确的最终位置,剩下的模拟所有的情况,注意这里只模拟结果,不模拟具体方法,具体方法数通过排列组合计算。
附上AC代码:

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
char dir[15];
double ans;
int pos,dpos,cnt;
int check(char a)//判断方向
{
    if(a=='+')
        return 1;
    else return -1;
}
int jie(int x)//计算阶乘
{
    if(x==0||x==1)return 1;
    else return x*jie(x-1);
}
int pl(int n,int m)//计算组合数
{
    return jie(n)/(jie(m)*jie(n-m));
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>dir)
    {
        pos=dpos=cnt=0;
        int len=strlen(dir);
        for(int i=0;i<len;i++)
        pos+=check(dir[i]);//得到正确位置
        for(int i=0;i<len;i++)
        {
            cin>>dir[i];
            if(dir[i]=='?')
                cnt++;//记录未知命令的个数
            else
                dpos+=check(dir[i]);
        }//得到按照s2已知命令走的位置
        if(cnt==0&&dpos==pos)
        {
            cout<<fixed<<setprecision(12)<<1.0<<endl;
            continue;
        }
        else if(cnt==0)
        {
            cout<<fixed<<setprecision(12)<<0.0<<endl;
            continue;
        }
        int sum=0,ok=0;
        for(int i=0;i<=cnt;i++)//处理未知命令
        {
            int res=dpos;
            res=res+cnt-2*i;
            if(res==pos)ok+=pl(cnt,i);//概率计算:组合数不再多讲
            sum+=pl(cnt,i);
        }
        cout<<fixed<<setprecision(12)<<(double)ok/(double)sum<<endl;
    }
    return 0;
}

欢迎评论!

猜你喜欢

转载自blog.csdn.net/wjl_zyl_1314/article/details/83478184
今日推荐