Artifact Content
Not logged in

Artifact bb06222c7a60f7be95e983d66d1dc517a30d0492


#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 T9 { public:
	string message(vector <string> part, vector <string> dict, vector <string> keystr) 
	{
		string key;
		for(int i=0; i<keystr.size(); ++i)
			key += keystr[i];

		sort(dict.begin(), dict.end());

		string answer;

		const char* p = key.c_str();
		while( *p )
		{
			if( *p == '0' ) {
				answer += ' ';
				++p;
			} else {
				vector<int> alp;
				while( '1' <= *p && *p <= '9' )
					alp.push_back( *p++ - '1' );
				int cnt = 0;
				while( *p=='*' || *p=='#' )
					cnt += (*p++=='*' ? 5 : 1);
				answer += decode(part, dict, alp, cnt);
			}
		}
		return answer;
	}

	string decode(const vector<string>& part, const vector<string>& dict, const vector<int>& alp, int cnt)
	{
		for(int i=0; i<dict.size(); ++i)
			if( match(dict[i], part, alp) )
				if( cnt-- == 0 )
					return dict[i];
		assert(false);
	}

	bool match(const string& d, const vector<string>& part, const vector<int>& alp)
	{
		if( d.size() != alp.size() )
			return false;

		for(int i=0; i<d.size(); ++i)
			if( part[alp[i]].find(d[i]) == string::npos )
				return false;
		return true;
	}
};

// 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 string& Expected, const string& 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(_, T9().message(part, dict, keystr));}
int main(){

CASE(0)
	string part_[] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
	  vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); 
	string dict_[] = {"bad"};
	  vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); 
	string keystr_[] = {"2230223"};
	  vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr_)); 
	string _ = "bad bad"; 
END
CASE(1)
	string part_[] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
	  vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); 
	string dict_[] = {"the", "tie"};
	  vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); 
	string keystr_[] = {"0843#000843#000"};
	  vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr_)); 
	string _ = " tie   tie   "; 
END
CASE(2)
	string part_[] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
	  vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); 
	string dict_[] = {"bad", "ace", "aad", "aae", "aaf", "acf", "acd", "the", "tie"};
	  vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); 
	string keystr_[] = {"223#02", "23*#00843#0"};
	  vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr_)); 
	string _ = "aae bad  tie "; 
END
CASE(3)
	string part_[] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
	  vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); 
	string dict_[] = {"the", "tie", "bad", "ace", "aad", "aae", "aaf", "acf", "acd"};
	  vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); 
	string keystr_[] = {"84300223#02", "23#*"};
	  vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr_)); 
	string _ = "the  aae bad"; 
END
CASE(4)
	string part_[] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
	  vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); 
	string dict_[] = {"bad", "ace", "aad", "aae", "tie", "aaf", "acf", "acd", "the"};
	  vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); 
	string keystr_[] = {"223#02", "23######"};
	  vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr_)); 
	string _ = "aae bad"; 
END
CASE(5)
	string part_[] = {"", "rq", "lde", "yoauz", "cbfgn", "tjkpx", "wvs", "ih", "m"};
	  vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); 
	string dict_[] = {"xktgmfmoqlmivm", 
 "hmthr", 
 "tpjgmnmaremiwm", 
 "tpjcmnmyrlmhvm", 
 "xkpnmgmzqdmhsm", 
 "wqopvvmiig", 
 "melbcbqeeg", 
 "jkxnmbmardmhwm", 
 "kpxnmcmyqlmism", 
 "wrztvsmhhf", 
 "srztssmiic", 
 "pxtgmfmyrdmhwm", 
 "vqoxswmiin", 
 "wryksvmihb", 
 "ptjfmbmoremhvm"};
	  vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); 
	string keystr_[] = {"00", 
 "7246779885##00000089682000007246779885##0000724677", 
 "9885#000089682000093355523350066659594239879###000"};
	  vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr_)); 
	string _ = "  wqopvvmiig      hmthr     wqopvvmiig    vqoxswmiin    hmthr    melbcbqeeg  pxtgmfmyrdmhwm   "; 
END
/*
CASE(6)
	string part_[] = ;
	  vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); 
	string dict_[] = ;
	  vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); 
	string keystr_[] = ;
	  vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr_)); 
	string _ = ; 
END
CASE(7)
	string part_[] = ;
	  vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); 
	string dict_[] = ;
	  vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); 
	string keystr_[] = ;
	  vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr_)); 
	string _ = ; 
END
*/
}
// END CUT HERE