[Assorted-commits] SF.net SVN: assorted:[1838] problems/topcoder
Brought to you by:
yangzhang
From: <yan...@us...> - 2012-08-15 17:22:50
|
Revision: 1838 http://assorted.svn.sourceforge.net/assorted/?rev=1838&view=rev Author: yangzhang Date: 2012-08-15 17:22:44 +0000 (Wed, 15 Aug 2012) Log Message: ----------- Add old missing TopCoder stabs Added Paths: ----------- problems/topcoder/BinaryCode/BinaryCode3.cpp problems/topcoder/BusinessTasks/BusinessTasks2.cpp Added: problems/topcoder/BinaryCode/BinaryCode3.cpp =================================================================== --- problems/topcoder/BinaryCode/BinaryCode3.cpp (rev 0) +++ problems/topcoder/BinaryCode/BinaryCode3.cpp 2012-08-15 17:22:44 UTC (rev 1838) @@ -0,0 +1,246 @@ +// vim:noet:sw=2:ts=2 + +// BEGIN CUT HERE +#if 0 +// PROBLEM STATEMENT +// Let's say you have a binary string such as the following: + +011100011 + +One way to encrypt this string is to add to each digit the sum of its adjacent digits. For example, the above string would become: + +123210122 + +In particular, if P is the original string, and Q is the encrypted string, then Q[i] = P[i-1] + P[i] + P[i+1] for all digit positions i. Characters off the left and right edges of the string are treated as zeroes. + +An encrypted string given to you in this format can be decoded as follows (using 123210122 as an example): + +Assume P[0] = 0. +Because Q[0] = P[0] + P[1] = 0 + P[1] = 1, we know that P[1] = 1. +Because Q[1] = P[0] + P[1] + P[2] = 0 + 1 + P[2] = 2, we know that P[2] = 1. +Because Q[2] = P[1] + P[2] + P[3] = 1 + 1 + P[3] = 3, we know that P[3] = 1. +Repeating these steps gives us P[4] = 0, P[5] = 0, P[6] = 0, P[7] = 1, and P[8] = 1. +We check our work by noting that Q[8] = P[7] + P[8] = 1 + 1 = 2. Since this equation works out, we are finished, and we have recovered one possible original string. + +Now we repeat the process, assuming the opposite about P[0]: + +Assume P[0] = 1. +Because Q[0] = P[0] + P[1] = 1 + P[1] = 0, we know that P[1] = 0. +Because Q[1] = P[0] + P[1] + P[2] = 1 + 0 + P[2] = 2, we know that P[2] = 1. +Now note that Q[2] = P[1] + P[2] + P[3] = 0 + 1 + P[3] = 3, which leads us to the conclusion that P[3] = 2. However, this violates the fact that each character in the original string must be '0' or '1'. Therefore, there exists no such original string P where the first digit is '1'. + +Note that this algorithm produces at most two decodings for any given encrypted string. There can never be more than one possible way to decode a string once the first binary digit is set. + +Given a string message, containing the encrypted string, return a vector <string> with exactly two elements. The first element should contain the decrypted string assuming the first character is '0'; the second element should assume the first character is '1'. If one of the tests fails, return the string "NONE" in its place. For the above example, you should return {"011100011", "NONE"}. + +DEFINITION +Class:BinaryCode +Method:decode +Parameters:string +Returns:vector <string> +Method signature:vector <string> decode(string message) + + +CONSTRAINTS +-message will contain between 1 and 50 characters, inclusive. +-Each character in message will be either '0', '1', '2', or '3'. + + +EXAMPLES + +0) +"123210122" + +Returns: { "011100011", "NONE" } + +The example from above. + +1) +"11" + +Returns: { "01", "10" } + +We know that one of the digits must be '1', and the other must be '0'. We return both cases. + +2) +"22111" + +Returns: { "NONE", "11001" } + +Since the first digit of the encrypted string is '2', the first two digits of the original string must be '1'. Our test fails when we try to assume that P[0] = 0. + +3) +"123210120" + +Returns: { "NONE", "NONE" } + +This is the same as the first example, but the rightmost digit has been changed to something inconsistent with the rest of the original string. No solutions are possible. + +4) +"3" + +Returns: { "NONE", "NONE" } + +5) +"12221112222221112221111111112221111" + +Returns: { "01101001101101001101001001001101001", "10110010110110010110010010010110010" } + +#endif +// END CUT HERE + +//#line 93 "BinaryCode.cpp" +#include <algorithm> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <string> +#include <vector> +using namespace std; + +// BEGIN CUT HERE +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) + +template<typename T> void print( T a ) { + cerr << a; +} +static void print( long long a ) { + cerr << a << "L"; +} +static void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + for ( int i = 0 ; i != a.size() ; i++ ) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +static void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) + +vector<string> split( const string& s, const string& delim =" " ) { + vector<string> res; + string t; + for ( int i = 0 ; i != s.size() ; i++ ) { + if ( delim.find( s[i] ) != string::npos ) { + if ( !t.empty() ) { + res.push_back( t ); + t = ""; + } + } else { + t += s[i]; + } + } + if ( !t.empty() ) { + res.push_back(t); + } + return res; +} + +vector<int> splitInt( const string& s, const string& delim =" " ) { + vector<string> tok = split( s, delim ); + vector<int> res; + for ( int i = 0 ; i != tok.size(); i++ ) + res.push_back( atoi( tok[i].c_str() ) ); + return res; +} + +class BinaryCode { + public: + vector <string> decode(string message) { + vector <string> res; + return res; + } +}; + +// BEGIN CUT HERE +int main() { + { + string retrunValueARRAY[] = { "011100011", "NONE" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(0, theObject.decode("123210122"),retrunValue); + } + { + string retrunValueARRAY[] = { "01", "10" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(1, theObject.decode("11"),retrunValue); + } + { + string retrunValueARRAY[] = { "NONE", "11001" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(2, theObject.decode("22111"),retrunValue); + } + { + string retrunValueARRAY[] = { "NONE", "NONE" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(3, theObject.decode("123210120"),retrunValue); + } + { + string retrunValueARRAY[] = { "NONE", "NONE" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(4, theObject.decode("3"),retrunValue); + } + { + string retrunValueARRAY[] = { "01101001101101001101001001001101001", "10110010110110010110010010010110010" }; + vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) ); + BinaryCode theObject; + eq(5, theObject.decode("12221112222221112221111111112221111"),retrunValue); + } + cin.get(); + return 0; +} +// END CUT HERE Added: problems/topcoder/BusinessTasks/BusinessTasks2.cpp =================================================================== --- problems/topcoder/BusinessTasks/BusinessTasks2.cpp (rev 0) +++ problems/topcoder/BusinessTasks/BusinessTasks2.cpp 2012-08-15 17:22:44 UTC (rev 1838) @@ -0,0 +1,238 @@ +// vim:noet:sw=2:ts=2 + +// BEGIN CUT HERE +#if 0 +// PROBLEM STATEMENT +// A busy businessman has a number of equally important tasks which he must accomplish. To decide which of the tasks to perform first, he performs the following operation. + +He writes down all his tasks in the form of a circular list, so the first task is adjacent to the last task. He then thinks of a positive number. This number is the random seed, which he calls n. Starting with the first task, he moves clockwise (from element 1 in the list to element 2 in the list and so on), counting from 1 to n. When his count reaches n, he removes that task from the list and starts counting from the next available task. He repeats this procedure until one task remains. It is this last task that he chooses to execute. + +Given a vector <string> list representing the tasks and an int n, return the task which the businessman chooses to execute. + + +DEFINITION +Class:BusinessTasks +Method:getTask +Parameters:vector <string>, int +Returns:string +Method signature:string getTask(vector <string> list, int n) + + +CONSTRAINTS +-list will contain between 2 and 50 elements inclusive. +-Each element in list will contain between 1 and 50 characters inclusive. +-Each element in list will contain only characters 'a'-'z'. +-n will be between 1 and 10000000 inclusive. + + +EXAMPLES + +0) +{"a","b","c","d"} +2 + +Returns: "a" + +We start counting from a. So a is 1, b is 2. We remove b, so list is now {a,c,d}. We continue from c. So c is 1, d is 2. We remove d, so list is now {a,c}. We continue from a. So a is 1, c is 2. We remove c, and now we are left with the last task a. + +1) +{"a","b","c","d","e"} +3 + +Returns: "d" + +We start counting from a. So a is 1, b is 2, c is 3. We remove c, now list = {a,b,d,e}. We continue from d. So d is 1, e is 2, a is 3. We remove a, now list = {b,d,e}. We continue from b. So b is 1, d is 2, e is 3. We remove e, now list = {b,d}. We continue from b. So b is 1, d is 2 and finally b is 3. We remove b, and now we are left with just one task d. + +2) +{"alpha","beta","gamma","delta","epsilon"} +1 + +Returns: "epsilon" + +3) +{"a","b"} +1000 + +Returns: "a" + +4) +{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t", +"u","v","w","x","y","z"} +17 + +Returns: "n" + +5) +{"zlqamum","yjsrpybmq","tjllfea","fxjqzznvg","nvhekxr","am","skmazcey","piklp", +"olcqvhg","dnpo","bhcfc","y","h","fj","bjeoaxglt","oafduixsz","kmtbaxu", +"qgcxjbfx","my","mlhy","bt","bo","q"} +9000000 + +Returns: "fxjqzznvg" + +#endif +// END CUT HERE + +//#line 77 "BusinessTasks.cpp" +#include <algorithm> +#include <cctype> +#include <cmath> +#include <cstdio> +#include <cstdlib> +#include <iostream> +#include <map> +#include <queue> +#include <set> +#include <sstream> +#include <string> +#include <vector> +using namespace std; + +// BEGIN CUT HERE +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) + +template<typename T> void print( T a ) { + cerr << a; +} +static void print( long long a ) { + cerr << a << "L"; +} +static void print( string a ) { + cerr << '"' << a << '"'; +} +template<typename T> void print( vector<T> a ) { + cerr << "{"; + for ( int i = 0 ; i != a.size() ; i++ ) { + if ( i != 0 ) cerr << ", "; + print( a[i] ); + } + cerr << "}" << endl; +} +template<typename T> void eq( int n, T have, T need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { + if( have.size() != need.size() ) { + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + for( size_t i= 0; i < have.size(); i++ ) { + if( have[i] != need[i] ) { + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "." << endl; + cerr << " have: "; print( have ); + cerr << " need: "; print( need ); + return; + } + } + cerr << "Case " << n << " passed." << endl; +} +static void eq( int n, string have, string need ) { + if ( have == need ) { + cerr << "Case " << n << " passed." << endl; + } else { + cerr << "Case " << n << " failed: expected "; + print( need ); + cerr << " received "; + print( have ); + cerr << "." << endl; + } +} +// END CUT HERE + +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) + +vector<string> split( const string& s, const string& delim =" " ) { + vector<string> res; + string t; + for ( int i = 0 ; i != s.size() ; i++ ) { + if ( delim.find( s[i] ) != string::npos ) { + if ( !t.empty() ) { + res.push_back( t ); + t = ""; + } + } else { + t += s[i]; + } + } + if ( !t.empty() ) { + res.push_back(t); + } + return res; +} + +vector<int> splitInt( const string& s, const string& delim =" " ) { + vector<string> tok = split( s, delim ); + vector<int> res; + for ( int i = 0 ; i != tok.size(); i++ ) + res.push_back( atoi( tok[i].c_str() ) ); + return res; +} + +class BusinessTasks { + public: + string getTask(vector <string> list, int n) { + int i = 0; + while (list.size() > 1) { + i = (i - 1 + n) % list.size(); + list.erase(list.begin() + i); + } + string res = list[0]; + return res; + } +}; + +// BEGIN CUT HERE +int main() { + { + string listARRAY[] = {"a","b","c","d"}; + vector <string> list( listARRAY, listARRAY+ARRSIZE(listARRAY) ); + BusinessTasks theObject; + eq(0, theObject.getTask(list, 2),"a"); + } + { + string listARRAY[] = {"a","b","c","d","e"}; + vector <string> list( listARRAY, listARRAY+ARRSIZE(listARRAY) ); + BusinessTasks theObject; + eq(1, theObject.getTask(list, 3),"d"); + } + { + string listARRAY[] = {"alpha","beta","gamma","delta","epsilon"}; + vector <string> list( listARRAY, listARRAY+ARRSIZE(listARRAY) ); + BusinessTasks theObject; + eq(2, theObject.getTask(list, 1),"epsilon"); + } + { + string listARRAY[] = {"a","b"}; + vector <string> list( listARRAY, listARRAY+ARRSIZE(listARRAY) ); + BusinessTasks theObject; + eq(3, theObject.getTask(list, 1000),"a"); + } + { + string listARRAY[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t", + "u","v","w","x","y","z"}; + vector <string> list( listARRAY, listARRAY+ARRSIZE(listARRAY) ); + BusinessTasks theObject; + eq(4, theObject.getTask(list, 17),"n"); + } + { + string listARRAY[] = {"zlqamum","yjsrpybmq","tjllfea","fxjqzznvg","nvhekxr","am","skmazcey","piklp", + "olcqvhg","dnpo","bhcfc","y","h","fj","bjeoaxglt","oafduixsz","kmtbaxu", + "qgcxjbfx","my","mlhy","bt","bo","q"}; + vector <string> list( listARRAY, listARRAY+ARRSIZE(listARRAY) ); + BusinessTasks theObject; + eq(5, theObject.getTask(list, 9000000),"fxjqzznvg"); + } + cin.get(); + return 0; +} +// END CUT HERE This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |