【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
input
output
input
output
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]。
*/
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]。
*/
相关文章推荐
- 用fail2ban阻止SSH和VSFTP暴力破解密码
- 2015BJOI day1第三题 糖果candy
- 4495: Least Prime factor 找到最小质因子P的第N小正整数
- 暴力枚举法求最大连续和
- CodeForces 241F Race
- POJ 2965.The Pilots Brothers' refrigerator
- POJ 1753.Flip Game
- POJ 3295.Tautology
- hdu 5533 Dancing Stars on Me
- hdu5532Almost Sorted Array
- 没参加的2015百度之星——数矩形
- poj 3279 Fliptile
- Birthday Cake——直线分割
- Spiral——找规律暴力
- POJ 1166&IOI 1994 The blocks 暴力枚举 解题报告
- hdu 1248
- LCM Challenge(暴力)
- 小晴天老师系列——竖式乘法(暴力)
- URAL 1786 Sandro's Biography
- URAL 1788 On the Benefits of Umbrellas