4e41a11c38 2011-06-18 kinaba: #include <iostream> 4e41a11c38 2011-06-18 kinaba: #include <sstream> 4e41a11c38 2011-06-18 kinaba: #include <iomanip> 4e41a11c38 2011-06-18 kinaba: #include <vector> 4e41a11c38 2011-06-18 kinaba: #include <string> 4e41a11c38 2011-06-18 kinaba: #include <map> 4e41a11c38 2011-06-18 kinaba: #include <set> 4e41a11c38 2011-06-18 kinaba: #include <algorithm> 4e41a11c38 2011-06-18 kinaba: #include <numeric> 4e41a11c38 2011-06-18 kinaba: #include <iterator> 4e41a11c38 2011-06-18 kinaba: #include <functional> 4e41a11c38 2011-06-18 kinaba: #include <complex> 4e41a11c38 2011-06-18 kinaba: #include <queue> 4e41a11c38 2011-06-18 kinaba: #include <stack> 4e41a11c38 2011-06-18 kinaba: #include <cmath> 4e41a11c38 2011-06-18 kinaba: #include <cassert> 4e41a11c38 2011-06-18 kinaba: #include <cstring> 4e41a11c38 2011-06-18 kinaba: using namespace std; 4e41a11c38 2011-06-18 kinaba: typedef long long LL; 4e41a11c38 2011-06-18 kinaba: typedef complex<double> CMP; 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: class PalindromizationDiv1 { public: 4e41a11c38 2011-06-18 kinaba: static const int INF = 0x3fffffff; 4e41a11c38 2011-06-18 kinaba: static const int NOLETTER = 26; 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: int getMinimumCost(string word, vector <string> operations) 4e41a11c38 2011-06-18 kinaba: { 4e41a11c38 2011-06-18 kinaba: // Cost for changing a character to another 4e41a11c38 2011-06-18 kinaba: int cost[27][27]; 4e41a11c38 2011-06-18 kinaba: for(int a=0; a<27; ++a) 4e41a11c38 2011-06-18 kinaba: for(int b=0; b<27; ++b) 4e41a11c38 2011-06-18 kinaba: cost[a][b] = (a==b ? 0 : INF); 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: for(size_t i=0; i<operations.size(); ++i) 4e41a11c38 2011-06-18 kinaba: { 4e41a11c38 2011-06-18 kinaba: stringstream cin(operations[i]); 4e41a11c38 2011-06-18 kinaba: string cmd; cin >> cmd; 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: char a, b; int c; 4e41a11c38 2011-06-18 kinaba: if( cmd == "add" ) { cin >> a >> c; cost[NOLETTER][ a - 'a'] = c; } 4e41a11c38 2011-06-18 kinaba: if( cmd == "erase" ) { cin >> a >> c; cost[ a - 'a'][NOLETTER] = c; } 4e41a11c38 2011-06-18 kinaba: if( cmd == "change" ){ cin >> a >> b >> c; cost[ a - 'a'][ b - 'a'] = c; } 4e41a11c38 2011-06-18 kinaba: } 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: // Warshall-Floyd 4e41a11c38 2011-06-18 kinaba: for(int k=0; k<27; ++k) 4e41a11c38 2011-06-18 kinaba: for(int i=0; i<27; ++i) 4e41a11c38 2011-06-18 kinaba: for(int j=0; j<27; ++j) 4e41a11c38 2011-06-18 kinaba: cost[i][j] = min(cost[i][j], cost[i][k]+cost[k][j]); 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: // Cost for matching two letters 4e41a11c38 2011-06-18 kinaba: int match_cost[27][27]; 4e41a11c38 2011-06-18 kinaba: for(int i=0; i<27; ++i) 4e41a11c38 2011-06-18 kinaba: for(int j=0; j<27; ++j) 4e41a11c38 2011-06-18 kinaba: { 4e41a11c38 2011-06-18 kinaba: match_cost[i][j] = INF; 4e41a11c38 2011-06-18 kinaba: for(int k=0; k<27; ++k) 4e41a11c38 2011-06-18 kinaba: match_cost[i][j] = min(match_cost[i][j], cost[i][k]+cost[j][k]); 4e41a11c38 2011-06-18 kinaba: } 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: // Solve 4e41a11c38 2011-06-18 kinaba: memo.clear(); 4e41a11c38 2011-06-18 kinaba: int r = rec(match_cost, word, 0, word.size()-1); 4e41a11c38 2011-06-18 kinaba: return (r>=INF ? -1 : r); 4e41a11c38 2011-06-18 kinaba: } 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: map<pair<int,int>,int> memo; 4e41a11c38 2011-06-18 kinaba: int rec(const int mc[27][27], const string& w, int s, int e) 4e41a11c38 2011-06-18 kinaba: { 4e41a11c38 2011-06-18 kinaba: // Base case 4e41a11c38 2011-06-18 kinaba: if( s >= e ) 4e41a11c38 2011-06-18 kinaba: return 0; 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: // Memoizaion 4e41a11c38 2011-06-18 kinaba: const pair<int,int> key(s, e); 4e41a11c38 2011-06-18 kinaba: if( memo.count(key) ) 4e41a11c38 2011-06-18 kinaba: return memo[key]; 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: // take best 4e41a11c38 2011-06-18 kinaba: int result = INF; 4e41a11c38 2011-06-18 kinaba: result = min(result, rec(mc, w, s+1, e-1)+mc[w[s]-'a'][w[e]-'a']); 4e41a11c38 2011-06-18 kinaba: result = min(result, rec(mc, w, s, e-1)+mc[NOLETTER][w[e]-'a']); 4e41a11c38 2011-06-18 kinaba: result = min(result, rec(mc, w, s+1, e )+mc[w[s]-'a'][NOLETTER]); 4e41a11c38 2011-06-18 kinaba: return memo[key] = result; 4e41a11c38 2011-06-18 kinaba: } 4e41a11c38 2011-06-18 kinaba: }; 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: // BEGIN CUT HERE 4e41a11c38 2011-06-18 kinaba: #include <ctime> 4e41a11c38 2011-06-18 kinaba: double start_time; string timer() 4e41a11c38 2011-06-18 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4e41a11c38 2011-06-18 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4e41a11c38 2011-06-18 kinaba: { os << "{ "; 4e41a11c38 2011-06-18 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4e41a11c38 2011-06-18 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4e41a11c38 2011-06-18 kinaba: void verify_case(const int& Expected, const int& Received) { 4e41a11c38 2011-06-18 kinaba: bool ok = (Expected == Received); 4e41a11c38 2011-06-18 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4e41a11c38 2011-06-18 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4e41a11c38 2011-06-18 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4e41a11c38 2011-06-18 kinaba: #define END verify_case(_, PalindromizationDiv1().getMinimumCost(word, operations));} 4e41a11c38 2011-06-18 kinaba: int main(){ 4e41a11c38 2011-06-18 kinaba: 4e41a11c38 2011-06-18 kinaba: CASE(0) 4e41a11c38 2011-06-18 kinaba: string word = "racecar"; 4e41a11c38 2011-06-18 kinaba: vector <string> operations; 4e41a11c38 2011-06-18 kinaba: int _ = 0; 4e41a11c38 2011-06-18 kinaba: END 4e41a11c38 2011-06-18 kinaba: CASE(1) 4e41a11c38 2011-06-18 kinaba: string word = "topcoder"; 4e41a11c38 2011-06-18 kinaba: string operations_[] = {"erase t 1", "erase o 1", "erase p 1", "erase c 1", "erase d 1", "erase e 1", "erase r 1"}; 4e41a11c38 2011-06-18 kinaba: vector <string> operations(operations_, operations_+sizeof(operations_)/sizeof(*operations_)); 4e41a11c38 2011-06-18 kinaba: int _ = 5; 4e41a11c38 2011-06-18 kinaba: END 4e41a11c38 2011-06-18 kinaba: CASE(2) 4e41a11c38 2011-06-18 kinaba: string word = "topcoder"; 4e41a11c38 2011-06-18 kinaba: string operations_[] = {"erase t 10", "erase o 1", "erase p 1", "erase c 1", "erase d 1", "erase e 1", "erase r 1"}; 4e41a11c38 2011-06-18 kinaba: vector <string> operations(operations_, operations_+sizeof(operations_)/sizeof(*operations_)); 4e41a11c38 2011-06-18 kinaba: int _ = 7; 4e41a11c38 2011-06-18 kinaba: END 4e41a11c38 2011-06-18 kinaba: CASE(3) 4e41a11c38 2011-06-18 kinaba: string word = "caaaaaab"; 4e41a11c38 2011-06-18 kinaba: string operations_[] = {"change b a 100000", "change c a 100000", "change c d 50000", "change b e 50000", "erase d 50000", "erase e 49999"}; 4e41a11c38 2011-06-18 kinaba: vector <string> operations(operations_, operations_+sizeof(operations_)/sizeof(*operations_)); 4e41a11c38 2011-06-18 kinaba: int _ = 199999; 4e41a11c38 2011-06-18 kinaba: END 4e41a11c38 2011-06-18 kinaba: CASE(4) 4e41a11c38 2011-06-18 kinaba: string word = "moon"; 4e41a11c38 2011-06-18 kinaba: string operations_[] = {"erase o 5", "add u 7", "change d p 3", "change m s 12", "change n d 6", "change s l 1"}; 4e41a11c38 2011-06-18 kinaba: vector <string> operations(operations_, operations_+sizeof(operations_)/sizeof(*operations_)); 4e41a11c38 2011-06-18 kinaba: int _ = -1; 4e41a11c38 2011-06-18 kinaba: END 4e41a11c38 2011-06-18 kinaba: CASE(5) 4e41a11c38 2011-06-18 kinaba: string word = "a"; 4e41a11c38 2011-06-18 kinaba: vector <string> operations; 4e41a11c38 2011-06-18 kinaba: int _ = 0; 4e41a11c38 2011-06-18 kinaba: END 4e41a11c38 2011-06-18 kinaba: CASE(6) // killer! 4e41a11c38 2011-06-18 kinaba: string word = "az"; 4e41a11c38 2011-06-18 kinaba: string operations_[] = {"add u 1", "change u b 100", "change a b 2"}; 4e41a11c38 2011-06-18 kinaba: vector <string> operations(operations_, operations_+sizeof(operations_)/sizeof(*operations_)); 4e41a11c38 2011-06-18 kinaba: int _ = 103; 4e41a11c38 2011-06-18 kinaba: END 4e41a11c38 2011-06-18 kinaba: CASE(7) // killer! 4e41a11c38 2011-06-18 kinaba: string word = "az"; 4e41a11c38 2011-06-18 kinaba: string operations_[] = {"add b 1", "change a b 1"}; 4e41a11c38 2011-06-18 kinaba: vector <string> operations(operations_, operations_+sizeof(operations_)/sizeof(*operations_)); 4e41a11c38 2011-06-18 kinaba: int _ = 2; 4e41a11c38 2011-06-18 kinaba: END 4e41a11c38 2011-06-18 kinaba: } 4e41a11c38 2011-06-18 kinaba: // END CUT HERE