9149ebcfa2 2014-02-07 kinaba: #include <iostream> 9149ebcfa2 2014-02-07 kinaba: #include <sstream> 9149ebcfa2 2014-02-07 kinaba: #include <iomanip> 9149ebcfa2 2014-02-07 kinaba: #include <vector> 9149ebcfa2 2014-02-07 kinaba: #include <string> 9149ebcfa2 2014-02-07 kinaba: #include <map> 9149ebcfa2 2014-02-07 kinaba: #include <set> 9149ebcfa2 2014-02-07 kinaba: #include <algorithm> 9149ebcfa2 2014-02-07 kinaba: #include <numeric> 9149ebcfa2 2014-02-07 kinaba: #include <iterator> 9149ebcfa2 2014-02-07 kinaba: #include <functional> 9149ebcfa2 2014-02-07 kinaba: #include <complex> 9149ebcfa2 2014-02-07 kinaba: #include <queue> 9149ebcfa2 2014-02-07 kinaba: #include <stack> 9149ebcfa2 2014-02-07 kinaba: #include <cmath> 9149ebcfa2 2014-02-07 kinaba: #include <cassert> 9149ebcfa2 2014-02-07 kinaba: #include <tuple> 9149ebcfa2 2014-02-07 kinaba: using namespace std; 9149ebcfa2 2014-02-07 kinaba: typedef long long LL; 9149ebcfa2 2014-02-07 kinaba: typedef complex<double> CMP; 9149ebcfa2 2014-02-07 kinaba: 9149ebcfa2 2014-02-07 kinaba: class PalindromicSubstringsDiv1 { public: 9149ebcfa2 2014-02-07 kinaba: double expectedPalindromes(vector <string> S1, vector <string> S2) 9149ebcfa2 2014-02-07 kinaba: { 9149ebcfa2 2014-02-07 kinaba: string A = accumulate(S1.begin(), S1.end(), string()); 9149ebcfa2 2014-02-07 kinaba: string B = accumulate(S2.begin(), S2.end(), string()); 9149ebcfa2 2014-02-07 kinaba: string S = A + B; 9149ebcfa2 2014-02-07 kinaba: return solve(S, S.size()); 9149ebcfa2 2014-02-07 kinaba: } 9149ebcfa2 2014-02-07 kinaba: 9149ebcfa2 2014-02-07 kinaba: double solve(const string& S, int N) 9149ebcfa2 2014-02-07 kinaba: { 9149ebcfa2 2014-02-07 kinaba: double e = 0.0; 9149ebcfa2 2014-02-07 kinaba: for(int c=0; c<N; ++c) 9149ebcfa2 2014-02-07 kinaba: { 9149ebcfa2 2014-02-07 kinaba: // odd 9149ebcfa2 2014-02-07 kinaba: double p = 1.0; 9149ebcfa2 2014-02-07 kinaba: e += p; // single-letter case 9149ebcfa2 2014-02-07 kinaba: for(int x=1; c-x>=0 && c+x<N; ++x) 9149ebcfa2 2014-02-07 kinaba: { 9149ebcfa2 2014-02-07 kinaba: if(S[c-x]=='?' || S[c+x]=='?') p*=1/26.0; 9149ebcfa2 2014-02-07 kinaba: else if(S[c-x] != S[c+x]) p=0; 9149ebcfa2 2014-02-07 kinaba: e += p; 9149ebcfa2 2014-02-07 kinaba: } 9149ebcfa2 2014-02-07 kinaba: // even 9149ebcfa2 2014-02-07 kinaba: p = 1.0; 9149ebcfa2 2014-02-07 kinaba: for(int x=1; c-x>=0 && c+x-1<N; ++x) 9149ebcfa2 2014-02-07 kinaba: { 9149ebcfa2 2014-02-07 kinaba: if(S[c-x]=='?' || S[c+x-1]=='?') p*=1/26.0; 9149ebcfa2 2014-02-07 kinaba: else if(S[c-x] != S[c+x-1]) p=0; 9149ebcfa2 2014-02-07 kinaba: e += p; 9149ebcfa2 2014-02-07 kinaba: } 9149ebcfa2 2014-02-07 kinaba: } 9149ebcfa2 2014-02-07 kinaba: return e; 9149ebcfa2 2014-02-07 kinaba: } 9149ebcfa2 2014-02-07 kinaba: }; 9149ebcfa2 2014-02-07 kinaba: 9149ebcfa2 2014-02-07 kinaba: // BEGIN CUT HERE 9149ebcfa2 2014-02-07 kinaba: #include <ctime> 9149ebcfa2 2014-02-07 kinaba: double start_time; string timer() 9149ebcfa2 2014-02-07 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 9149ebcfa2 2014-02-07 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 9149ebcfa2 2014-02-07 kinaba: { os << "{ "; 9149ebcfa2 2014-02-07 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 9149ebcfa2 2014-02-07 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 9149ebcfa2 2014-02-07 kinaba: void verify_case(const double& Expected, const double& Received) { 9149ebcfa2 2014-02-07 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 9149ebcfa2 2014-02-07 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 9149ebcfa2 2014-02-07 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 9149ebcfa2 2014-02-07 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 9149ebcfa2 2014-02-07 kinaba: #define END verify_case(_, PalindromicSubstringsDiv1().expectedPalindromes(S1, S2));} 9149ebcfa2 2014-02-07 kinaba: int main(){ 9149ebcfa2 2014-02-07 kinaba: 9149ebcfa2 2014-02-07 kinaba: CASE(0) 9149ebcfa2 2014-02-07 kinaba: string S1_[] = {"a","a",""}; 9149ebcfa2 2014-02-07 kinaba: vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 9149ebcfa2 2014-02-07 kinaba: string S2_[] = {"a"}; 9149ebcfa2 2014-02-07 kinaba: vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 9149ebcfa2 2014-02-07 kinaba: double _ = 6.0; 9149ebcfa2 2014-02-07 kinaba: END 9149ebcfa2 2014-02-07 kinaba: CASE(1) 9149ebcfa2 2014-02-07 kinaba: string S1_[] = {"z??"}; 9149ebcfa2 2014-02-07 kinaba: vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 9149ebcfa2 2014-02-07 kinaba: vector <string> S2; 9149ebcfa2 2014-02-07 kinaba: double _ = 3.115384615384615; 9149ebcfa2 2014-02-07 kinaba: END 9149ebcfa2 2014-02-07 kinaba: CASE(2) 9149ebcfa2 2014-02-07 kinaba: string S1_[] = {"ab","c"}; 9149ebcfa2 2014-02-07 kinaba: vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 9149ebcfa2 2014-02-07 kinaba: string S2_[] = {"??","a?"}; 9149ebcfa2 2014-02-07 kinaba: vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 9149ebcfa2 2014-02-07 kinaba: double _ = 7.315088757396449; 9149ebcfa2 2014-02-07 kinaba: END 9149ebcfa2 2014-02-07 kinaba: CASE(3) 9149ebcfa2 2014-02-07 kinaba: vector <string> S1; 9149ebcfa2 2014-02-07 kinaba: string S2_[] = {"?"}; 9149ebcfa2 2014-02-07 kinaba: vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 9149ebcfa2 2014-02-07 kinaba: double _ = 1.0; 9149ebcfa2 2014-02-07 kinaba: END 9149ebcfa2 2014-02-07 kinaba: CASE(4) 9149ebcfa2 2014-02-07 kinaba: string S1_[] = {"ab?def","?"}; 9149ebcfa2 2014-02-07 kinaba: vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 9149ebcfa2 2014-02-07 kinaba: string S2_[] = {"f??a"}; 9149ebcfa2 2014-02-07 kinaba: vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 9149ebcfa2 2014-02-07 kinaba: double _ = 12.545971779699588; 9149ebcfa2 2014-02-07 kinaba: END 9149ebcfa2 2014-02-07 kinaba: /* 9149ebcfa2 2014-02-07 kinaba: CASE(5) 9149ebcfa2 2014-02-07 kinaba: string S1_[] = ; 9149ebcfa2 2014-02-07 kinaba: vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 9149ebcfa2 2014-02-07 kinaba: string S2_[] = ; 9149ebcfa2 2014-02-07 kinaba: vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 9149ebcfa2 2014-02-07 kinaba: double _ = ; 9149ebcfa2 2014-02-07 kinaba: END 9149ebcfa2 2014-02-07 kinaba: CASE(6) 9149ebcfa2 2014-02-07 kinaba: string S1_[] = ; 9149ebcfa2 2014-02-07 kinaba: vector <string> S1(S1_, S1_+sizeof(S1_)/sizeof(*S1_)); 9149ebcfa2 2014-02-07 kinaba: string S2_[] = ; 9149ebcfa2 2014-02-07 kinaba: vector <string> S2(S2_, S2_+sizeof(S2_)/sizeof(*S2_)); 9149ebcfa2 2014-02-07 kinaba: double _ = ; 9149ebcfa2 2014-02-07 kinaba: END 9149ebcfa2 2014-02-07 kinaba: */ 9149ebcfa2 2014-02-07 kinaba: } 9149ebcfa2 2014-02-07 kinaba: // END CUT HERE