7cbc6cb50e 2013-05-25 kinaba: #include <iostream> 7cbc6cb50e 2013-05-25 kinaba: #include <sstream> 7cbc6cb50e 2013-05-25 kinaba: #include <iomanip> 7cbc6cb50e 2013-05-25 kinaba: #include <vector> 7cbc6cb50e 2013-05-25 kinaba: #include <string> 7cbc6cb50e 2013-05-25 kinaba: #include <map> 7cbc6cb50e 2013-05-25 kinaba: #include <set> 7cbc6cb50e 2013-05-25 kinaba: #include <algorithm> 7cbc6cb50e 2013-05-25 kinaba: #include <numeric> 7cbc6cb50e 2013-05-25 kinaba: #include <iterator> 7cbc6cb50e 2013-05-25 kinaba: #include <functional> 7cbc6cb50e 2013-05-25 kinaba: #include <complex> 7cbc6cb50e 2013-05-25 kinaba: #include <queue> 7cbc6cb50e 2013-05-25 kinaba: #include <stack> 7cbc6cb50e 2013-05-25 kinaba: #include <cmath> 7cbc6cb50e 2013-05-25 kinaba: #include <cassert> 7cbc6cb50e 2013-05-25 kinaba: using namespace std; 7cbc6cb50e 2013-05-25 kinaba: typedef long long LL; 7cbc6cb50e 2013-05-25 kinaba: typedef long double LD; 7cbc6cb50e 2013-05-25 kinaba: typedef complex<double> CMP; 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: class UndoHistory { public: 7cbc6cb50e 2013-05-25 kinaba: int minPresses(vector <string> lines) 7cbc6cb50e 2013-05-25 kinaba: { 7cbc6cb50e 2013-05-25 kinaba: int score = lines[0].size() + 1; 7cbc6cb50e 2013-05-25 kinaba: for(int i=1; i<lines.size(); ++i) 7cbc6cb50e 2013-05-25 kinaba: { 7cbc6cb50e 2013-05-25 kinaba: int best = 0x7fffffff; 7cbc6cb50e 2013-05-25 kinaba: for(int k=0; k<i; ++k) { 7cbc6cb50e 2013-05-25 kinaba: int pm = prefix_match(lines[i], lines[k]); 7cbc6cb50e 2013-05-25 kinaba: if(k<i-1) { 7cbc6cb50e 2013-05-25 kinaba: best = min<int>(best, 2 + lines[i].size()-pm); 7cbc6cb50e 2013-05-25 kinaba: } else { 7cbc6cb50e 2013-05-25 kinaba: if(pm == lines[k].size()) 7cbc6cb50e 2013-05-25 kinaba: best = min<int>(best, lines[i].size() - pm); 7cbc6cb50e 2013-05-25 kinaba: else 7cbc6cb50e 2013-05-25 kinaba: best = min<int>(best, 2 + lines[i].size() - pm); 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: score += best+1; 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: return score; 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: int prefix_match(const string& a, const string& b) 7cbc6cb50e 2013-05-25 kinaba: { 7cbc6cb50e 2013-05-25 kinaba: int i = 0; 7cbc6cb50e 2013-05-25 kinaba: while(i<a.size() && i<b.size() && a[i]==b[i]) 7cbc6cb50e 2013-05-25 kinaba: ++i; 7cbc6cb50e 2013-05-25 kinaba: return i; 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: }; 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: // BEGIN CUT HERE 7cbc6cb50e 2013-05-25 kinaba: #include <ctime> 7cbc6cb50e 2013-05-25 kinaba: double start_time; string timer() 7cbc6cb50e 2013-05-25 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 7cbc6cb50e 2013-05-25 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 7cbc6cb50e 2013-05-25 kinaba: { os << "{ "; 7cbc6cb50e 2013-05-25 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 7cbc6cb50e 2013-05-25 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 7cbc6cb50e 2013-05-25 kinaba: void verify_case(const int& Expected, const int& Received) { 7cbc6cb50e 2013-05-25 kinaba: bool ok = (Expected == Received); 7cbc6cb50e 2013-05-25 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 7cbc6cb50e 2013-05-25 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 7cbc6cb50e 2013-05-25 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 7cbc6cb50e 2013-05-25 kinaba: #define END verify_case(_, UndoHistory().minPresses(lines));} 7cbc6cb50e 2013-05-25 kinaba: int main(){ 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: CASE(0) 7cbc6cb50e 2013-05-25 kinaba: string lines_[] = {"tomorrow", "topcoder"}; 7cbc6cb50e 2013-05-25 kinaba: vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = 18; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: CASE(1) 7cbc6cb50e 2013-05-25 kinaba: string lines_[] = {"a","b"}; 7cbc6cb50e 2013-05-25 kinaba: vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = 6; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: CASE(2) 7cbc6cb50e 2013-05-25 kinaba: string lines_[] = {"a", "ab", "abac", "abacus" }; 7cbc6cb50e 2013-05-25 kinaba: vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = 10; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: CASE(3) 7cbc6cb50e 2013-05-25 kinaba: string lines_[] = {"pyramid", "sphinx", "sphere", "python", "serpent"}; 7cbc6cb50e 2013-05-25 kinaba: vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = 39; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: CASE(4) 7cbc6cb50e 2013-05-25 kinaba: string lines_[] = {"ba","a","a","b","ba"} 7cbc6cb50e 2013-05-25 kinaba: ; 7cbc6cb50e 2013-05-25 kinaba: vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = 13; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: /* 7cbc6cb50e 2013-05-25 kinaba: CASE(5) 7cbc6cb50e 2013-05-25 kinaba: string lines_[] = ; 7cbc6cb50e 2013-05-25 kinaba: vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = ; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: CASE(6) 7cbc6cb50e 2013-05-25 kinaba: string lines_[] = ; 7cbc6cb50e 2013-05-25 kinaba: vector <string> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = ; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: */ 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: // END CUT HERE