ADDED SRM/428/2A.cpp Index: SRM/428/2A.cpp ================================================================== --- SRM/428/2A.cpp +++ SRM/428/2A.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __GNUC__ +#include +#define unordered_map __gnu_cxx::hash_map +#else +#include +#endif +using namespace std; +typedef long long LL; +typedef complex CMP; + +class ThePalindrome { public: + int find(string s) + { + for(int i=0; i +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, ThePalindrome().find(s));} +int main(){ + +CASE(0) + string s = "abab"; + int _ = 5; +END +CASE(1) + string s = "abacaba"; + int _ = 7; +END +CASE(2) + string s = "qwerty"; + int _ = 11; +END +CASE(3) + string s = "abdfhdyrbdbsdfghjkllkjhgfds"; + int _ = 38; +END +/* +CASE(4) + string s = ; + int _ = ; +END +CASE(5) + string s = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/428/2C.cpp Index: SRM/428/2C.cpp ================================================================== --- SRM/428/2C.cpp +++ SRM/428/2C.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __GNUC__ +#include +#define unordered_map __gnu_cxx::hash_map +#else +#include +#endif +using namespace std; +typedef long long LL; +typedef complex CMP; + +class TheDictionary { public: + string find(int n, int m, int k) + { + if( k <= C(n+m,n) ) + return rec(n, m, k); + return ""; + } + + string rec(int n, int m, int k) + { + if( n+m == 0 ) + return ""; + if( n==0 ) + return "z" + rec(n, m-1, k); + if( m==0 ) + return "a" + rec(n-1, m, k); + + LL c = C(n+m-1, m); + if( k <= c ) + return "a" + rec(n-1, m, k); + return "z" + rec(n, m-1, k - c); + } + + LL C(int n, int k) + { + k = min(k, n-k); + LL v = 1; + for(int i=1; i<=k; ++i) { + v = v * (n+1-i) / i; + if( v >= (1LL<<50) ) + return v; + } + return v; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, TheDictionary().find(n, m, k));} +int main(){ + +CASE(0) + int n = 2; + int m = 2; + int k = 2; + string _ = "azaz"; +END +CASE(1) + int n = 2; + int m = 2; + int k = 6; + string _ = "zzaa"; +END +CASE(2) + int n = 10; + int m = 10; + int k = 1000000000; + string _ = ""; +END +CASE(3) + int n = 7; + int m = 4; + int k = 47; + string _ = "aaazazaazaz"; +END +/* +CASE(4) + int n = ; + int m = ; + int k = ; + string _ = ; +END +CASE(5) + int n = ; + int m = ; + int k = ; + string _ = ; +END +*/ +} +// END CUT HERE