Artifact Content
Not logged in

Artifact db310718265ebf137f1f906d0855f80defb4c2bd


#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 PalindromicSubstringsDiv1 { public:
	double expectedPalindromes(vector <string> S1, vector <string> S2)
	{
		string A = accumulate(S1.begin(), S1.end(), string());
		string B = accumulate(S2.begin(), S2.end(), string());
		string S = A + B;
		return solve(S, S.size());
	}

	double solve(const string& S, int N)
	{
		double e = 0.0;
		for(int c=0; c<N; ++c)
		{
			// odd
			double p = 1.0;
			e += p;  // single-letter case
			for(int x=1; c-x>=0 && c+x<N; ++x)
			{
				if(S[c-x]=='?' || S[c+x]=='?') p*=1/26.0;
				else if(S[c-x] != S[c+x]) p=0;
				e += p;
			}
			// even
			p = 1.0;
			for(int x=1; c-x>=0 && c+x-1<N; ++x)
			{
				if(S[c-x]=='?' || S[c+x-1]=='?') p*=1/26.0;
				else if(S[c-x] != S[c+x-1]) p=0;
				e += p;
			}
		}
		return e;
	}
};

// 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(_, PalindromicSubstringsDiv1().expectedPalindromes(S1, S2));}
int main(){

CASE(0)
	string S1_[] = {"a","a",""};
	  vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 
	string S2_[] = {"a"};
	  vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 
	double _ = 6.0; 
END
CASE(1)
	string S1_[] = {"z??"};
	  vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 
	vector <string> S2; 
	double _ = 3.115384615384615; 
END
CASE(2)
	string S1_[] = {"ab","c"};
	  vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 
	string S2_[] = {"??","a?"};
	  vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 
	double _ = 7.315088757396449; 
END
CASE(3)
	vector <string> S1; 
	string S2_[] = {"?"};
	  vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 
	double _ = 1.0; 
END
CASE(4)
	string S1_[] = {"ab?def","?"};
	  vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 
	string S2_[] = {"f??a"};
	  vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 
	double _ = 12.545971779699588; 
END
/*
CASE(5)
	string S1_[] = ;
	  vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 
	string S2_[] = ;
	  vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 
	double _ = ; 
END
CASE(6)
	string S1_[] = ;
	  vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 
	string S2_[] = ;
	  vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 
	double _ = ; 
END
*/
}
// END CUT HERE