4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class QuickT9 { public: 4fd800b3a8 2011-02-23 kinaba: int minimumPressings(vector <string> t9, string word) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: map< string, set<string> > D2U; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<t9.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: stringstream sin(t9[i]); 4fd800b3a8 2011-02-23 kinaba: for(string u;sin>>u;) { 4fd800b3a8 2011-02-23 kinaba: string d; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<u.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: if(u[i]<='c') d+='2'; 4fd800b3a8 2011-02-23 kinaba: else if(u[i]<='f') d+='3'; 4fd800b3a8 2011-02-23 kinaba: else if(u[i]<='i') d+='4'; 4fd800b3a8 2011-02-23 kinaba: else if(u[i]<='l') d+='5'; 4fd800b3a8 2011-02-23 kinaba: else if(u[i]<='o') d+='6'; 4fd800b3a8 2011-02-23 kinaba: else if(u[i]<='s') d+='7'; 4fd800b3a8 2011-02-23 kinaba: else if(u[i]<='v') d+='8'; 4fd800b3a8 2011-02-23 kinaba: else if(u[i]<='z') d+='9'; 4fd800b3a8 2011-02-23 kinaba: D2U[d].insert( u.substr(0,i+1) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return rec(D2U, word, 0, "", ""); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // Now, the state is F=word[0..i), D=D, U=U. What is the minimal number of steps from here? 4fd800b3a8 2011-02-23 kinaba: // It is computed by simply implementing the problem statement, with memoization. 4fd800b3a8 2011-02-23 kinaba: map<pair<int,string>, int> memo; 4fd800b3a8 2011-02-23 kinaba: int rec( map< string, set<string> >& D2U, const string& word, int i, const string& D, const string& U ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( i == word.size() ) return 0; 4fd800b3a8 2011-02-23 kinaba: if( i < 0 ) return -1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: pair<int,string> key(i,U); 4fd800b3a8 2011-02-23 kinaba: if( memo.count(key) ) 4fd800b3a8 2011-02-23 kinaba: return memo[key]; 4fd800b3a8 2011-02-23 kinaba: memo[key] = -1; // avoid cyclic dependency 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int best = -1; 4fd800b3a8 2011-02-23 kinaba: #define UPDATE(s,k) { if(s!=-1 && (best==-1 || s+(k)<best)) best=s+(k); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // try RIGHT 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int cp = 0; 4fd800b3a8 2011-02-23 kinaba: while( i+cp<word.size() && cp<U.size() && word[i+cp] == U[cp] ) 4fd800b3a8 2011-02-23 kinaba: ++cp; 4fd800b3a8 2011-02-23 kinaba: int k = U.size()-cp; 4fd800b3a8 2011-02-23 kinaba: int s = rec(D2U, word, i+U.size()-k, "", ""); 4fd800b3a8 2011-02-23 kinaba: UPDATE(s,k+1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // try C 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int cp = 0; 4fd800b3a8 2011-02-23 kinaba: while( i+cp<word.size() && cp<U.size() && word[i+cp] == U[cp] ) 4fd800b3a8 2011-02-23 kinaba: ++cp; 4fd800b3a8 2011-02-23 kinaba: int k = U.size()-cp; 4fd800b3a8 2011-02-23 kinaba: if(k==0) k=1; // the only difference is here 4fd800b3a8 2011-02-23 kinaba: int s = rec(D2U, word, i+U.size()-k, "", ""); 4fd800b3a8 2011-02-23 kinaba: UPDATE(s,k); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // try * 4fd800b3a8 2011-02-23 kinaba: if( !D.empty() ) { 4fd800b3a8 2011-02-23 kinaba: set<string>& Us = D2U[D]; 4fd800b3a8 2011-02-23 kinaba: set<string>::iterator it = Us.find(U); 4fd800b3a8 2011-02-23 kinaba: ++it; 4fd800b3a8 2011-02-23 kinaba: if( it == Us.end() ) 4fd800b3a8 2011-02-23 kinaba: it = Us.begin(); 4fd800b3a8 2011-02-23 kinaba: int s = rec(D2U, word, i, D, *it); 4fd800b3a8 2011-02-23 kinaba: UPDATE(s,1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // try 2-9 4fd800b3a8 2011-02-23 kinaba: for(char c='2'; c<='9'; ++c) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: string nextD = D + c; 4fd800b3a8 2011-02-23 kinaba: if( D2U.count(nextD) ) { 4fd800b3a8 2011-02-23 kinaba: set<string>& Us = D2U[nextD]; 4fd800b3a8 2011-02-23 kinaba: int s = rec(D2U, word, i, nextD, *Us.begin()); 4fd800b3a8 2011-02-23 kinaba: UPDATE(s,1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // done 4fd800b3a8 2011-02-23 kinaba: return memo[key] = best; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, QuickT9().minimumPressings(t9, word));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: string t9_[] = {"aae", "bab", "abad", "bdbd", "beta"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 4fd800b3a8 2011-02-23 kinaba: string word = "babe"; 4fd800b3a8 2011-02-23 kinaba: int _ = 9; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string t9_[] = {"ann","ie"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 4fd800b3a8 2011-02-23 kinaba: string word = "annie"; 4fd800b3a8 2011-02-23 kinaba: int _ = 7; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string t9_[] = {"ann","amm"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 4fd800b3a8 2011-02-23 kinaba: string word = "annie"; 4fd800b3a8 2011-02-23 kinaba: int _ = -1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string t9_[] = {"aaa aab","aac aba abb ccca"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 4fd800b3a8 2011-02-23 kinaba: string word = "aba"; 4fd800b3a8 2011-02-23 kinaba: int _ = 6; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string t9_[] = {"acac aba aaab","aab aa baa","bba bacade abb","baba"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 4fd800b3a8 2011-02-23 kinaba: string word = "abbaca"; 4fd800b3a8 2011-02-23 kinaba: int _ = 10; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: string t9_[] = {"aaa aab aac","aba abb","ccca"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 4fd800b3a8 2011-02-23 kinaba: string word = "ccc"; 4fd800b3a8 2011-02-23 kinaba: int _ = 5; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: string t9_[] = {"abc"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 4fd800b3a8 2011-02-23 kinaba: string word = "a"; 4fd800b3a8 2011-02-23 kinaba: int _ = 2; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(7) 4fd800b3a8 2011-02-23 kinaba: string t9_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 4fd800b3a8 2011-02-23 kinaba: string word = ; 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE