您的位置:首页 > 大数据 > 人工智能

【Codeforces Round 272 (Div 2)B】【暴力dfs or 组合数】Dreamoon and WiFi 问号填加减方案数使得最后恰好增量为aim

2015-12-25 22:23 609 查看
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:

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 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<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=0x3f3f3f3f;
int casenum,casei;
char a[12],b[12];
int aim,num,tmp,ans;
void dfs(int p,int tmp)
{
if(b[p]==0)
{
if(tmp==aim)++ans;
return;
}
if(b[p]=='+'||b[p]=='?')
{
dfs(p+1,tmp+1);
}
if(b[p]=='-'||b[p]=='?')
{
dfs(p+1,tmp-1);
}
}
void way1()
{
ans=0;
dfs(0,0);
printf("%.15f\n",1.0*ans/(1<<num));
}
int C(int n,int m)
{
int c[12][12];MS(c,0);
c[0][0]=1;
for(int i=1;i<=n;++i)
{
c[i][0]=1;
for(int j=1;j<=i;++j)
{
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
return c
[m];
}
void way2()
{
int dis=aim-tmp;
int x=num+dis>>1;
int y=num-dis>>1;
if(x>=0&&y>=0&&x+y==num)printf("%.15f\n",1.0*C(num,x)/(1<<num));
else puts("0");
}
int main()
{
while(~scanf("%s%s",a,b))
{
aim=0;
for(int i=0;a[i];++i)
{
if(a[i]=='+')++aim;
else --aim;
}
tmp=0;
num=0;
for(int i=0;b[i];++i)
{
if(b[i]=='+')++tmp;
else if(b[i]=='-')--tmp;
else ++num;
}
//way1();
way2();
}
return 0;
}
/*
【trick&&吐槽】
做题一定要注意观察数据规模,选择最适合的算法就是最好的哦。

【题意】
给你两个等长的串。
第一个串只含 '+' 和 '-',代表 +1 或 -1,于是会有一个最终的增量aim
第二个串含有 '+' 个 '-',还有'?',我们可以任意填充'?',想使得第二个串的最终增量也恰好为aim
问填充方案数

【类型】
暴力dfs or 组合数

【分析】
方法一:暴力dfs
数据太小,暴力dfs不要太舒服。

方法二:组合数
设aim-tmp的差值为dis。
设加号x个,减号y个,那么有——
x+y==num
x-y==dis
x=num+dis>>1
y=num-dis>>1
求出x和y,如果合理,答案就是c[num][x]。

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息