Artifact e6519d12c758090275d7734ac06ede1d211be339
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class PalindromePermutations { public:
double palindromeProbability(string word)
{
map<char, int> cnt;
for(char c: word) cnt[c]++;
vector<int> ns;
for(auto cn: cnt) ns.emplace_back(cn.second);
return solve(ns);
}
double solve(vector<int> c)
{
if(count_if(c.begin(), c.end(), [&](int x){return x%2==1;}) >= 2)
return 0.0;
return rec(c);
}
double rec(vector<int> c)
{
if(c.size() <= 1)
return 1.0;
int n = accumulate(c.begin(), c.end(), 0);
int n2 = n/2;
int k = c.back();
int k2 = k/2;
c.pop_back();
return C(n2,k2) / C(n,k) * rec(c);
}
double C(int n, int k)
{
double v = 1.0;
for(int i=1; i<=k; ++i)
v = v * (n+1-i) / i;
return v;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const double& Expected, const double& Received) {
bool ok = (abs(Expected - Received) < 1e-9);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, PalindromePermutations().palindromeProbability(word));}
int main(){
CASE(0)
string word = "haha";
double _ = 0.3333333333333333;
END
CASE(1)
string word = "xxxxy";
double _ = 0.2;
END
CASE(2)
string word = "xxxx";
double _ = 1.0;
END
CASE(3)
string word = "abcde";
double _ = 0.0;
END
CASE(4)
string word = "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhff";
double _ = 0.025641025641025637;
END
/*
CASE(5)
string word = ;
double _ = ;
END
CASE(6)
string word = ;
double _ = ;
END
*/
}
// END CUT HERE