14e9f37735 2012-01-14 kinaba: #include <iostream> 14e9f37735 2012-01-14 kinaba: #include <sstream> 14e9f37735 2012-01-14 kinaba: #include <iomanip> 14e9f37735 2012-01-14 kinaba: #include <vector> 14e9f37735 2012-01-14 kinaba: #include <string> 14e9f37735 2012-01-14 kinaba: #include <map> 14e9f37735 2012-01-14 kinaba: #include <set> 14e9f37735 2012-01-14 kinaba: #include <algorithm> 14e9f37735 2012-01-14 kinaba: #include <numeric> 14e9f37735 2012-01-14 kinaba: #include <iterator> 14e9f37735 2012-01-14 kinaba: #include <functional> 14e9f37735 2012-01-14 kinaba: #include <complex> 14e9f37735 2012-01-14 kinaba: #include <queue> 14e9f37735 2012-01-14 kinaba: #include <stack> 14e9f37735 2012-01-14 kinaba: #include <cmath> 14e9f37735 2012-01-14 kinaba: #include <cassert> 14e9f37735 2012-01-14 kinaba: #include <cstring> 14e9f37735 2012-01-14 kinaba: #ifdef __GNUC__ 14e9f37735 2012-01-14 kinaba: #include <ext/hash_map> 14e9f37735 2012-01-14 kinaba: #define unordered_map __gnu_cxx::hash_map 14e9f37735 2012-01-14 kinaba: #else 14e9f37735 2012-01-14 kinaba: #include <unordered_map> 14e9f37735 2012-01-14 kinaba: #endif 14e9f37735 2012-01-14 kinaba: using namespace std; 14e9f37735 2012-01-14 kinaba: typedef long long LL; 14e9f37735 2012-01-14 kinaba: typedef complex<double> CMP; 14e9f37735 2012-01-14 kinaba: 14e9f37735 2012-01-14 kinaba: class MinCostPalindrome { public: 14e9f37735 2012-01-14 kinaba: int getMinimum(string s, int oCost, int xCost) 14e9f37735 2012-01-14 kinaba: { 14e9f37735 2012-01-14 kinaba: int total = 0; 14e9f37735 2012-01-14 kinaba: for(int i=0; i<s.size()-i-1; ++i) 14e9f37735 2012-01-14 kinaba: if( s[i]=='o' && s[s.size()-i-1]=='x' ) 14e9f37735 2012-01-14 kinaba: return -1; 14e9f37735 2012-01-14 kinaba: else if( s[i]=='x' && s[s.size()-i-1]=='o' ) 14e9f37735 2012-01-14 kinaba: return -1; 14e9f37735 2012-01-14 kinaba: else if( s[i]=='o' && s[s.size()-i-1]=='?' 14e9f37735 2012-01-14 kinaba: || s[i]=='?' && s[s.size()-i-1]=='o' ) 14e9f37735 2012-01-14 kinaba: total += oCost; 14e9f37735 2012-01-14 kinaba: else if( s[i]=='x' && s[s.size()-i-1]=='?' 14e9f37735 2012-01-14 kinaba: || s[i]=='?' && s[s.size()-i-1]=='x' ) 14e9f37735 2012-01-14 kinaba: total += xCost; 14e9f37735 2012-01-14 kinaba: else if( s[i]=='?' && s[s.size()-i-1]=='?' ) 14e9f37735 2012-01-14 kinaba: total += min(oCost, xCost) * 2; 14e9f37735 2012-01-14 kinaba: if( s.size()%2 == 1 && s[s.size()/2]=='?' ) 14e9f37735 2012-01-14 kinaba: total += min(oCost, xCost); 14e9f37735 2012-01-14 kinaba: return total; 14e9f37735 2012-01-14 kinaba: } 14e9f37735 2012-01-14 kinaba: }; 14e9f37735 2012-01-14 kinaba: 14e9f37735 2012-01-14 kinaba: // BEGIN CUT HERE 14e9f37735 2012-01-14 kinaba: #include <ctime> 14e9f37735 2012-01-14 kinaba: double start_time; string timer() 14e9f37735 2012-01-14 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 14e9f37735 2012-01-14 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 14e9f37735 2012-01-14 kinaba: { os << "{ "; 14e9f37735 2012-01-14 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 14e9f37735 2012-01-14 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 14e9f37735 2012-01-14 kinaba: void verify_case(const int& Expected, const int& Received) { 14e9f37735 2012-01-14 kinaba: bool ok = (Expected == Received); 14e9f37735 2012-01-14 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 14e9f37735 2012-01-14 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 14e9f37735 2012-01-14 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 14e9f37735 2012-01-14 kinaba: #define END verify_case(_, MinCostPalindrome().getMinimum(s, oCost, xCost));} 14e9f37735 2012-01-14 kinaba: int main(){ 14e9f37735 2012-01-14 kinaba: 14e9f37735 2012-01-14 kinaba: CASE(0) 14e9f37735 2012-01-14 kinaba: string s = "oxo?xox?"; 14e9f37735 2012-01-14 kinaba: int oCost = 3; 14e9f37735 2012-01-14 kinaba: int xCost = 5; 14e9f37735 2012-01-14 kinaba: int _ = 8; 14e9f37735 2012-01-14 kinaba: END 14e9f37735 2012-01-14 kinaba: CASE(1) 14e9f37735 2012-01-14 kinaba: string s = "x??x"; 14e9f37735 2012-01-14 kinaba: int oCost = 9; 14e9f37735 2012-01-14 kinaba: int xCost = 4; 14e9f37735 2012-01-14 kinaba: int _ = 8; 14e9f37735 2012-01-14 kinaba: END 14e9f37735 2012-01-14 kinaba: CASE(2) 14e9f37735 2012-01-14 kinaba: string s = "ooooxxxx"; 14e9f37735 2012-01-14 kinaba: int oCost = 12; 14e9f37735 2012-01-14 kinaba: int xCost = 34; 14e9f37735 2012-01-14 kinaba: int _ = -1; 14e9f37735 2012-01-14 kinaba: END 14e9f37735 2012-01-14 kinaba: CASE(3) 14e9f37735 2012-01-14 kinaba: string s = "oxoxooxxxxooxoxo"; 14e9f37735 2012-01-14 kinaba: int oCost = 7; 14e9f37735 2012-01-14 kinaba: int xCost = 4; 14e9f37735 2012-01-14 kinaba: int _ = 0; 14e9f37735 2012-01-14 kinaba: END 14e9f37735 2012-01-14 kinaba: CASE(4) 14e9f37735 2012-01-14 kinaba: string s = "?o"; 14e9f37735 2012-01-14 kinaba: int oCost = 6; 14e9f37735 2012-01-14 kinaba: int xCost = 2; 14e9f37735 2012-01-14 kinaba: int _ = 6; 14e9f37735 2012-01-14 kinaba: END 14e9f37735 2012-01-14 kinaba: CASE(5) 14e9f37735 2012-01-14 kinaba: string s = "????????????????????"; 14e9f37735 2012-01-14 kinaba: int oCost = 50; 14e9f37735 2012-01-14 kinaba: int xCost = 49; 14e9f37735 2012-01-14 kinaba: int _ = 980; 14e9f37735 2012-01-14 kinaba: END 14e9f37735 2012-01-14 kinaba: CASE(6) 14e9f37735 2012-01-14 kinaba: string s = "o??oxxo?xoox?ox??x??" ; 14e9f37735 2012-01-14 kinaba: int oCost = 3; 14e9f37735 2012-01-14 kinaba: int xCost = 10; 14e9f37735 2012-01-14 kinaba: int _ = 38; 14e9f37735 2012-01-14 kinaba: END 14e9f37735 2012-01-14 kinaba: /* 14e9f37735 2012-01-14 kinaba: CASE(7) 14e9f37735 2012-01-14 kinaba: string s = ; 14e9f37735 2012-01-14 kinaba: int oCost = ; 14e9f37735 2012-01-14 kinaba: int xCost = ; 14e9f37735 2012-01-14 kinaba: int _ = ; 14e9f37735 2012-01-14 kinaba: END 14e9f37735 2012-01-14 kinaba: CASE(8) 14e9f37735 2012-01-14 kinaba: string s = ; 14e9f37735 2012-01-14 kinaba: int oCost = ; 14e9f37735 2012-01-14 kinaba: int xCost = ; 14e9f37735 2012-01-14 kinaba: int _ = ; 14e9f37735 2012-01-14 kinaba: END 14e9f37735 2012-01-14 kinaba: */ 14e9f37735 2012-01-14 kinaba: } 14e9f37735 2012-01-14 kinaba: // END CUT HERE