226a376552 2011-12-28 kinaba: #include <iostream> 226a376552 2011-12-28 kinaba: #include <sstream> 226a376552 2011-12-28 kinaba: #include <iomanip> 226a376552 2011-12-28 kinaba: #include <vector> 226a376552 2011-12-28 kinaba: #include <string> 226a376552 2011-12-28 kinaba: #include <map> 226a376552 2011-12-28 kinaba: #include <set> 226a376552 2011-12-28 kinaba: #include <algorithm> 226a376552 2011-12-28 kinaba: #include <numeric> 226a376552 2011-12-28 kinaba: #include <iterator> 226a376552 2011-12-28 kinaba: #include <functional> 226a376552 2011-12-28 kinaba: #include <complex> 226a376552 2011-12-28 kinaba: #include <queue> 226a376552 2011-12-28 kinaba: #include <stack> 226a376552 2011-12-28 kinaba: #include <cmath> 226a376552 2011-12-28 kinaba: #include <cassert> 226a376552 2011-12-28 kinaba: #include <cstring> 226a376552 2011-12-28 kinaba: #ifdef __GNUC__ 226a376552 2011-12-28 kinaba: #include <ext/hash_map> 226a376552 2011-12-28 kinaba: #define unordered_map __gnu_cxx::hash_map 226a376552 2011-12-28 kinaba: #else 226a376552 2011-12-28 kinaba: #include <unordered_map> 226a376552 2011-12-28 kinaba: #endif 226a376552 2011-12-28 kinaba: using namespace std; 226a376552 2011-12-28 kinaba: typedef long long LL; 226a376552 2011-12-28 kinaba: typedef complex<double> CMP; 226a376552 2011-12-28 kinaba: 226a376552 2011-12-28 kinaba: class MagicNaming { public: 226a376552 2011-12-28 kinaba: int maxReindeers(string magicName) 226a376552 2011-12-28 kinaba: { 226a376552 2011-12-28 kinaba: return rec(magicName, 0, ""); 226a376552 2011-12-28 kinaba: } 226a376552 2011-12-28 kinaba: 226a376552 2011-12-28 kinaba: map<pair<int,string>, int> memo; 226a376552 2011-12-28 kinaba: int rec(const string& s, int i, const string& last) 226a376552 2011-12-28 kinaba: { 226a376552 2011-12-28 kinaba: if( i == s.size() ) 226a376552 2011-12-28 kinaba: return 0; 226a376552 2011-12-28 kinaba: pair<int,string> key(i, last); 226a376552 2011-12-28 kinaba: if( memo.count(key) ) 226a376552 2011-12-28 kinaba: return memo[key]; 226a376552 2011-12-28 kinaba: 226a376552 2011-12-28 kinaba: // a+b <= b+a, b+c <= c+b 226a376552 2011-12-28 kinaba: int score = -1; 226a376552 2011-12-28 kinaba: for(int e=i+1; e<=s.size(); ++e) { 226a376552 2011-12-28 kinaba: string t = s.substr(i, e-i); 226a376552 2011-12-28 kinaba: if( last+t <= t+last ) { 226a376552 2011-12-28 kinaba: int ss = rec(s, e, t); 226a376552 2011-12-28 kinaba: if( ss != -1 ) 226a376552 2011-12-28 kinaba: score = max(score, 1 + ss); 226a376552 2011-12-28 kinaba: } 226a376552 2011-12-28 kinaba: } 226a376552 2011-12-28 kinaba: return memo[key] = score; 226a376552 2011-12-28 kinaba: } 226a376552 2011-12-28 kinaba: }; 226a376552 2011-12-28 kinaba: 226a376552 2011-12-28 kinaba: // BEGIN CUT HERE 226a376552 2011-12-28 kinaba: #include <ctime> 226a376552 2011-12-28 kinaba: double start_time; string timer() 226a376552 2011-12-28 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 226a376552 2011-12-28 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 226a376552 2011-12-28 kinaba: { os << "{ "; 226a376552 2011-12-28 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 226a376552 2011-12-28 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 226a376552 2011-12-28 kinaba: void verify_case(const int& Expected, const int& Received) { 226a376552 2011-12-28 kinaba: bool ok = (Expected == Received); 226a376552 2011-12-28 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 226a376552 2011-12-28 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 226a376552 2011-12-28 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 226a376552 2011-12-28 kinaba: #define END verify_case(_, MagicNaming().maxReindeers(magicName));} 226a376552 2011-12-28 kinaba: int main(){ 226a376552 2011-12-28 kinaba: 226a376552 2011-12-28 kinaba: CASE(0) 226a376552 2011-12-28 kinaba: string magicName = "aba"; 226a376552 2011-12-28 kinaba: int _ = 2; 226a376552 2011-12-28 kinaba: END 226a376552 2011-12-28 kinaba: CASE(1) 226a376552 2011-12-28 kinaba: string magicName = "babbaba"; 226a376552 2011-12-28 kinaba: int _ = 2; 226a376552 2011-12-28 kinaba: END 226a376552 2011-12-28 kinaba: CASE(2) 226a376552 2011-12-28 kinaba: string magicName = "philosophersstone"; 226a376552 2011-12-28 kinaba: int _ = 5; 226a376552 2011-12-28 kinaba: END 226a376552 2011-12-28 kinaba: CASE(3) 226a376552 2011-12-28 kinaba: string magicName = "knuthmorrispratt"; 226a376552 2011-12-28 kinaba: int _ = 7; 226a376552 2011-12-28 kinaba: END 226a376552 2011-12-28 kinaba: CASE(4) 226a376552 2011-12-28 kinaba: string magicName = "acrushpetrtourist"; 226a376552 2011-12-28 kinaba: int _ = 7; 226a376552 2011-12-28 kinaba: END 226a376552 2011-12-28 kinaba: CASE(5) 226a376552 2011-12-28 kinaba: string magicName = "zzzzz"; 226a376552 2011-12-28 kinaba: int _ = 5; 226a376552 2011-12-28 kinaba: END 226a376552 2011-12-28 kinaba: /* 226a376552 2011-12-28 kinaba: CASE(6) 226a376552 2011-12-28 kinaba: string magicName = ; 226a376552 2011-12-28 kinaba: int _ = ; 226a376552 2011-12-28 kinaba: END 226a376552 2011-12-28 kinaba: CASE(7) 226a376552 2011-12-28 kinaba: string magicName = ; 226a376552 2011-12-28 kinaba: int _ = ; 226a376552 2011-12-28 kinaba: END 226a376552 2011-12-28 kinaba: */ 226a376552 2011-12-28 kinaba: } 226a376552 2011-12-28 kinaba: // END CUT HERE