Skip to content

Instantly share code, notes, and snippets.

@irfanabduhu
Created February 6, 2020 05:42
Show Gist options
  • Save irfanabduhu/19052615e55a9a0a1bd59e7ac00fc4e3 to your computer and use it in GitHub Desktop.
Save irfanabduhu/19052615e55a9a0a1bd59e7ac00fc4e3 to your computer and use it in GitHub Desktop.
Codeforces
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main(void)
{
string s1, s2;
cin >> s1 >> s2;
int n = s1.length(); // length of the signal
int p1 = 0, m1 = 0; // count of plus and minus in s1
int p2 = 0, m2 = 0, q = 0; // count of plus, minus, and unrecognized in s2
for (auto c : s1)
if (c == '+')
p1++;
else
m1++;
for (auto c : s2)
if (c == '+')
p2++;
else if (c == '-')
m2++;
else
q++;
if (p2 + m2 == n)
{ // all commands are recognized
if (p1 - m1 == p2 - m2)
cout << 1.0 << endl;
else
cout << 0.0 << endl;
}
else
{ // some commands are not recognized
int d = abs(p1 - m1 - p2 + m2);
if (d > q || (d % 2 != q % 2))
cout << 0.0 << endl;
else
{
int x = (q - d) / 2;
double denominator = pow(2, q);
double numerator = 1; // Combination(q, x)
for (int i = 0; i < x; i++)
{
numerator *= q - i;
numerator /= i + 1;
}
cout.precision(10);
cout << numerator / denominator << endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment