Artifact Content
Not logged in

Artifact 71780af0d20da0b94840a488e28f9f0b1af62cc2


#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>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class MagicNaming { public:
	int maxReindeers(string magicName)
	{
		return rec(magicName, 0, "");
	}

	map<pair<int,string>, int> memo;
	int rec(const string& s, int i, const string& last)
	{
		if( i == s.size() )
			return 0;
		pair<int,string> key(i, last);
		if( memo.count(key) )
			return memo[key];

		// a+b <= b+a,  b+c <= c+b
		int score = -1;
		for(int e=i+1; e<=s.size(); ++e) {
			string t = s.substr(i, e-i);
			if( last+t <= t+last ) {
				int ss = rec(s, e, t);
				if( ss != -1 )
					score = max(score, 1 + ss);
			}
		}
		return memo[key] = score;
	}
};

// 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(_, MagicNaming().maxReindeers(magicName));}
int main(){

CASE(0)
	string magicName = "aba"; 
	int _ = 2; 
END
CASE(1)
	string magicName = "babbaba"; 
	int _ = 2; 
END
CASE(2)
	string magicName = "philosophersstone"; 
	int _ = 5; 
END
CASE(3)
	string magicName = "knuthmorrispratt"; 
	int _ = 7; 
END
CASE(4)
	string magicName = "acrushpetrtourist"; 
	int _ = 7; 
END
CASE(5)
	string magicName = "zzzzz"; 
	int _ = 5; 
END
/*
CASE(6)
	string magicName = ; 
	int _ = ; 
END
CASE(7)
	string magicName = ; 
	int _ = ; 
END
*/
}
// END CUT HERE