http://codeforces.com/contest/476/problem/B

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.

Sample test(s)
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.

解题思路: 求接收到的信号与初始信号直接相同的概率

所以只要记录下接收到的有几个不确定的(即‘?’的个数),然后依次枚举即可

#include <stdio.h>
#include <string.h>
#include <stdlib.h> int main(){
    char str1[], str2[];
    int num1[], num2[], len, sum, i, t, x, y, n, m, flag;
    while(scanf("%s %s", str1, str2) != EOF){
        len =strlen(str1);
        sum = ;
        for(i = ; i < len; i++){
            num1[i] = str1[i] == '+' ?  : ;
            if(str2[i] == '+'){
                num2[i] = ;
            }
            else if(str2[i] == '-'){
                num2[i] = ;
            }
            else if(str2[i] == '?'){
                sum++;
            }
        }
        x = ;
        flag = ;
        for(i = ; i < len; i++){
            x += num1[i] ==  ?  : - ;
        }
        for(i = ; i < sum; i++){
            flag *= ;
        }
        t = flag;
        n = m = ;
        while(t > ){
            for(i = ; i < len; i++){
                if(str2[i] == '?'){
                    if(t %  == ){
                        num2[i] = ;
                    }
                    else{
                        num2[i] = ;
                    }
                    t /= ;
                }
            }
            y = ;
            for(i = ; i < len; i++){
                y += (num2[i] ==  ?  : -);
            }
            t = --flag;
            if(x == y){
                n++;
            }
            m++;
        }
        printf("%.12lf\n",(double)n / (double)m);
    }
    return ;
}

Codeforces Round #272 (Div. 2)-B. Dreamoon and WiFi的更多相关文章

  1. 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 ...

  2. Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi (暴力二进制枚举)

    题意:给你一个只含\(+\)和\(-\)的字符串,统计它的加减和,然后再给你一个包含\(+,-,?\)的字符串,其中\(?\)可以表示为\(+\)或\(-\),问有多少种情况使得第二个字符串的加减和等 ...

  3. Codeforces Round #272 (Div. 2) E. Dreamoon and Strings 动态规划

    E. Dreamoon and Strings 题目连接: http://www.codeforces.com/contest/476/problem/E Description Dreamoon h ...

  4. Codeforces Round #272 (Div. 2) D. Dreamoon and Sets 构造

    D. Dreamoon and Sets 题目连接: http://www.codeforces.com/contest/476/problem/D Description Dreamoon like ...

  5. Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs 水题

    A. Dreamoon and Stairs 题目连接: http://www.codeforces.com/contest/476/problem/A Description Dreamoon wa ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

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

随机推荐

  1. 利用StringBuffer来替换内容

    package com.test.java; public class StringBufferTest { public static void main(String[] args) { Stri ...

  2. Redis缓存雪崩、缓存穿透、缓存击穿、缓存降级、缓存预热、缓存更新

    Redis缓存能够有效地加速应用的读写速度,就DB来说,Redis成绩已经很惊人了,且不说memcachedb和Tokyo Cabinet之流,就说原版的memcached,速度似乎也只能达到这个级别 ...

  3. CSS小技巧收藏

    居中对齐 很多时候我们需要把一个元素在其父级容器里水平.垂直居中对齐.以下我列出了常用的几种方法: 1.在知道子元素宽度与高度的情况下进行居中,采用位置定位:absolute + margin .pa ...

  4. iTween基础之iTweenPath浅析(自定义路径移动)

    http://www.2cto.com/kf/201604/498023.html 在游戏开发中经常会用到让一个游戏对象按照指定的路线移动,iTweenPath就提供了可视化的编辑路径功能. iTwe ...

  5. Suse 11 sp4 安装Oracle 11g

    环境信息 suse 11 sp4 64位 企业版配置:虚拟机,1U4CORE,4GB内存,100GB存储(实际可用空间20GB) linux环境配置修改 在安装oracle之前,需要先对linux 的 ...

  6. 小奇的仓库(树形DP)

    「题目背景」 小奇采的矿实在太多了,它准备在喵星系建个矿石仓库.令它无语的是,喵星系的货运飞船引擎还停留在上元时代! 「问题描述」 喵星系有n个星球,星球以及星球间的航线形成一棵树. 从星球a到星球b ...

  7. Win7下Intellij开发Scala环境搭建

    1.Scala下载并安装 1.Scala的安装时需要依赖JDK的,目前我的电脑上,jdk是已经安装好了,这里就不再说明 2.在地址http://www.scala-lang.org/download/ ...

  8. LVS 负载均衡器总结

    下面部分原理部分,是从网上摘录,源网址已经无从获取,我将其中一小部分模糊的说明加入了一些自己的理解,仅最大可能让全文容易阅读,也方便自己以后参考,若你是大牛希望能给我一些宝贵的建议,将理解有误的地方加 ...

  9. mybatis-trim标签说明

    trim标签使用1.trim 有四个属性 2.prefix,suffix 表示在trim标签包裹的部分的前面或者后面添加内容(注意:是没有prefixOverrides,suffixOverrides ...

  10. 5 天 4000 star 的一个爆款开源项目

    今天早上起来浏览 GitHub 的时候,在周热门趋势排行榜上看到了这么一个开源项目,仅仅 5 天时间,爬到了周排行榜的第一名的位置.而在每天的排行榜上,今天一早也高高位居排行榜的第二位. 这个开源项目 ...