Artifact Content
Not logged in

Artifact 67cc5158338e6e25439951846737ee840f234380


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class QuickT9 { public:
	int minimumPressings(vector <string> t9, string word) 
	{
		map< string, set<string> > D2U;
		for(int i=0; i<t9.size(); ++i) {
			stringstream sin(t9[i]);
			for(string u;sin>>u;) {
				string d;
				for(int i=0; i<u.size(); ++i) {
					if(u[i]<='c') d+='2';
					else if(u[i]<='f') d+='3';
					else if(u[i]<='i') d+='4';
					else if(u[i]<='l') d+='5';
					else if(u[i]<='o') d+='6';
					else if(u[i]<='s') d+='7';
					else if(u[i]<='v') d+='8';
					else if(u[i]<='z') d+='9';
					D2U[d].insert( u.substr(0,i+1) );
				}
			}
		}
		return rec(D2U, word, 0, "", "");
	}

	// Now, the state is F=word[0..i), D=D, U=U. What is the minimal number of steps from here?
	// It is computed by simply implementing the problem statement, with memoization.
	map<pair<int,string>, int> memo;
	int rec( map< string, set<string> >& D2U, const string& word, int i, const string& D, const string& U )
	{
		if( i == word.size() ) return 0;
		if( i < 0 )            return -1;

		pair<int,string> key(i,U);
		if( memo.count(key) )
			return memo[key];
		memo[key] = -1; // avoid cyclic dependency

		int best = -1;
		#define UPDATE(s,k) { if(s!=-1 && (best==-1 || s+(k)<best)) best=s+(k); }

		// try RIGHT
		{
			int cp = 0;
			while( i+cp<word.size() && cp<U.size() && word[i+cp] == U[cp] )
				++cp;
			int k = U.size()-cp;
			int s = rec(D2U, word, i+U.size()-k, "", "");
			UPDATE(s,k+1);
		}
		// try C
		{
			int cp = 0;
			while( i+cp<word.size() && cp<U.size() && word[i+cp] == U[cp] )
				++cp;
			int k = U.size()-cp;
			if(k==0) k=1; // the only difference is here
			int s = rec(D2U, word, i+U.size()-k, "", "");
			UPDATE(s,k);
		}
		// try *
		if( !D.empty() ) {
			set<string>& Us = D2U[D];
			set<string>::iterator it = Us.find(U);
			++it;
			if( it == Us.end() )
				it = Us.begin();
			int s = rec(D2U, word, i, D, *it);
			UPDATE(s,1);
		}
		// try 2-9
		for(char c='2'; c<='9'; ++c)
		{
			string nextD = D + c;
			if( D2U.count(nextD) ) {
				set<string>& Us = D2U[nextD];
				int s = rec(D2U, word, i, nextD, *Us.begin());
				UPDATE(s,1);
			}
		}
		// done
		return memo[key] = best;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, QuickT9().minimumPressings(t9, word));}
int main(){

CASE(0)
	string t9_[] = {"aae", "bab", "abad", "bdbd", "beta"};
	  vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 
	string word = "babe"; 
	int _ = 9; 
END
CASE(1)
	string t9_[] = {"ann","ie"};
	  vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 
	string word = "annie"; 
	int _ = 7; 
END
CASE(2)
	string t9_[] = {"ann","amm"};
	  vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 
	string word = "annie"; 
	int _ = -1; 
END
CASE(3)
	string t9_[] = {"aaa aab","aac aba abb ccca"};
	  vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 
	string word = "aba"; 
	int _ = 6; 
END
CASE(4)
	string t9_[] = {"acac aba aaab","aab aa baa","bba bacade abb","baba"};
	  vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 
	string word = "abbaca"; 
	int _ = 10; 
END
CASE(5)
	string t9_[] = {"aaa aab aac","aba abb","ccca"};
	  vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 
	string word = "ccc"; 
	int _ = 5; 
END
CASE(6)
string t9_[] = {"abc"};
	  vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 
	string word = "a"; 
	int _ = 2; 
END
/*
CASE(7)
	string t9_[] = ;
	  vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); 
	string word = ; 
	int _ = ; 
END
*/
}
// END CUT HERE