TopCoder 2 Lottery

Problem Statement

 

In most states, gamblers can choose from a wide variety of different lottery games. The rules of a lottery are defined by two integers (choices and blanks) and two boolean variables (sorted and unique). choices represents the highest valid number that you may use on your lottery ticket. (All integers between 1 and choices, inclusive, are valid and can appear on your ticket.) blanks represents the number of spots on your ticket where numbers can be written.

The sorted and unique variables indicate restrictions on the tickets you can create. If sorted is set to true, then the numbers on your ticket must be written in non-descending order. If sorted is set to false, then the numbers may be written in any order. Likewise, if unique is set to true, then each number you write on your ticket must be distinct. If unique is set to false, then repeats are allowed.

Here are some example lottery tickets, where choices = 15 and blanks = 4:

  • {3, 7, 12, 14} -- this ticket is unconditionally valid.
  • {13, 4, 1, 9} -- because the numbers are not in nondescending order, this ticket is valid only if sorted = false.
  • {8, 8, 8, 15} -- because there are repeated numbers, this ticket is valid only if unique = false.
  • {11, 6, 2, 6} -- this ticket is valid only if sorted = false and unique = false.

Given a list of lotteries and their corresponding rules, return a list of lottery names sorted by how easy they are to win. The probability that you will win a lottery is equal to (1 / (number of valid lottery tickets for that game)). The easiest lottery to win should appear at the front of the list. Ties should be broken alphabetically (see example 1).

Definition

 
Class: Lottery
Method: sortByOdds
Parameters: vector <string>
Returns: vector <string>
Method signature: vector <string> sortByOdds(vector <string> rules)
(be sure your method is public)
 
 

Constraints

- rules will contain between 0 and 50 elements, inclusive.
- Each element of rules will contain between 11 and 50 characters, inclusive.
- Each element of rules will be in the format "<NAME>:_<CHOICES>_<BLANKS>_<SORTED>_<UNIQUE>" (quotes for clarity). The underscore character represents exactly one space. The string will have no leading or trailing spaces.
- <NAME> will contain between 1 and 40 characters, inclusive, and will consist of only uppercase letters ('A'-'Z') and spaces (' '), with no leading or trailing spaces.
- <CHOICES> will be an integer between 10 and 100, inclusive, with no leading zeroes.
- <BLANKS> will be an integer between 1 and 8, inclusive, with no leading zeroes.
- <SORTED> will be either 'T' (true) or 'F' (false).
- <UNIQUE> will be either 'T' (true) or 'F' (false).
- No two elements in rules will have the same name.

Examples

0)  
 
{"PICK ANY TWO: 10 2 F F"
,"PICK TWO IN ORDER: 10 2 T F"
,"PICK TWO DIFFERENT: 10 2 F T"
,"PICK TWO LIMITED: 10 2 T T"}
Returns: 
{ "PICK TWO LIMITED",
  "PICK TWO IN ORDER",
  "PICK TWO DIFFERENT",
  "PICK ANY TWO" }

The "PICK ANY TWO" game lets either blank be a number from 1 to 10. Therefore, there are 10 * 10 = 100 possible tickets, and your odds of winning are 1/100.

The "PICK TWO IN ORDER" game means that the first number cannot be greater than the second number. This eliminates 45 possible tickets, leaving us with 55 valid ones. The odds of winning are 1/55.

The "PICK TWO DIFFERENT" game only disallows tickets where the first and second numbers are the same. There are 10 such tickets, leaving the odds of winning at 1/90.

Finally, the "PICK TWO LIMITED" game disallows an additional 10 tickets from the 45 disallowed in "PICK TWO IN ORDER". The odds of winning this game are 1/45.

1)  
 
{"INDIGO: 93 8 T F",
 "ORANGE: 29 8 F T",
 "VIOLET: 76 6 F F",
 "BLUE: 100 8 T T",
 "RED: 99 8 T T",
 "GREEN: 78 6 F T",
 "YELLOW: 75 6 F F"}
Returns: { "RED",  "ORANGE",  "YELLOW",  "GREEN",  "BLUE",  "INDIGO",  "VIOLET" }

Note that INDIGO and BLUE both have the exact same odds (1/186087894300). BLUE is listed first because it comes before INDIGO alphabetically.

2)  
 
{}
Returns: { }

Empty case

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     

#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <exception>
#include <iostream>
using namespace std;
struct LotteryInfo
{
	string Name;
	int Chocies;
	int Blanks;
	bool Sorted;
	bool Unique;
	string Odds;
};

class Lottery
{
private:
	string Sum(string str1, string str2)
	{
		size_t found;
		found = str1.find_first_not_of('0');
		if (found != string::npos)
		{
			str1 = str1.substr(found, str1.length()-found);
		}
		found = str2.find_first_not_of('0');
		if (found != string::npos)
		{
			str2 = str2.substr(found, str2.length()-found);
		}

		string a, b, c = "";
		if (str1.length() >= str2.length())//a is bigger than b
		{
			a = str1;
			b = str2;
		}
		else
		{
			a = str2;
			b = str1;
		}
		c.assign(a.length()+1, '0');
		for (size_t i = 0; i < a.length(); i++)
		{
			if (i >= b.length())
			{
				c.at(c.length()-1-i) += (a.at(a.length()-1-i)-'0');
				continue;
			}
			else
			{
				c.at(c.length()-1-i) += ((a.at(a.length()-1-i)-'0') + (b.at(b.length()-1-i)-'0'));
			}
		}
		for (int i = c.length()-1; i >= 0; i--)
		{
			int n = i;
			while (c.at(n) > '9')
			{
				c.at(n) -= 10;
				c.at(n-1) += 1;
			}
		}
		found = c.find_first_not_of('0');
		if (found != string::npos)
		{
			c = c.substr(found, c.length()-found);
		}
		return c;
	}
	string Sub(string str1, string str2)//assume str1 is bigger than str2
	{
		size_t found;
		found = str1.find_first_not_of('0');
		if (found != string::npos)
		{
			str1 = str1.substr(found, str1.length()-found);
		}
		found = str2.find_first_not_of('0');
		if (found != string::npos)
		{
			str2 = str2.substr(found, str2.length()-found);
		}

		if (str1.length() < str2.length())
		{
			return "";
		}
		else if (str1.length() == str2.length())
		{
			if (str1.compare(str2) < 0)
			{
				return "";
			}
			else if (str1.compare(str2) == 0)
			{
				return "0";
			}
		}

		string result;
		result.assign(str1.length(), '0');
		for (size_t i = 0; i< str1.length(); i++)
		{
			if (i >= str2.length())//assume str1 is bigger than str2
			{
				result.at(result.length()-1-i) = str1.at(str1.length()-1-i);
				continue;
			}
			result.at(result.length()-1-i) += (str1.at(str1.length()-1-i) - str2.at(str2.length()-1-i));
			if (result.at(result.length()-1-i) < '0')
			{
				int n = str1.length()-1-i-1;
				str1.at(n)--;
				while (str1.at(n) < '0')
				{
					str1.at(n) = '9';
					n--;
					if (n < 0)
					{
						return "";
					}
					str1.at(n)--;
				}
				result.at(result.length()-1-i) += 10;
			}
		}
		found = result.find_first_not_of('0');
		if (found != string::npos)
			result = result.substr(found, result.length()-found);
		return result;
	}
	string Mult(string str1, string str2)
	{
		size_t found;
		found = str1.find_first_not_of('0');
		if (found != string::npos)
		{
			str1 = str1.substr(found, str1.length()-found);
		}
		found = str2.find_first_not_of('0');
		if (found != string::npos)
		{
			str2 = str2.substr(found, str2.length()-found);
		}

		string a, b, c = "";
		if (str1.length() >= str2.length())
		{
			a = str1;
			b = str2;
		}
		else
		{
			a = str2;
			b = str1;
		}
		c.assign(a.length()+b.length(), '0');
		size_t a_length = a.length();
		size_t b_length = b.length();
		for (size_t i = 0; i < b_length; i++)
		{
			for (size_t j = 0; j < a_length; j++)
			{
				int mul = (b.at(b_length-1-i)-'0') * (a.at(a_length-1-j)-'0');
				c.at(c.length()-1-i-j) += mul%10;
				c.at(c.length()-1-i-j-1) += mul/10;
				if (c.at(c.length()-1-i-j) > '9')
				{
					int n = c.length()-1-i-j;
					while (c.at(n) > '9')
					{
						int h_bit = (c.at(n)-'0')/10;
						c.at(n) -= h_bit*10;
						n--;
						c.at(n) += h_bit;
					}
				}
			}
		}
		found = c.find_first_not_of('0');
		if (found != string::npos)
			c = c.substr(found, c.length()-found);
		return c;
	}
	string Division(string str1, string str2)//assume str1 is bigger than str2
	{
		size_t found;
		found = str1.find_first_not_of('0');
		if (found != string::npos)
			str1 = str1.substr(found, str1.length()-found);
		else
		{
			if (str1.length() > 1)
			{
				found = str1.find_first_of('0');
				if (found != string::npos)
				{
					return "0";
				}
			}
		}
		found = str2.find_first_not_of('0');
		if (found != string::npos)
			str2 = str2.substr(found, str2.length()-found);

		if (str1.length() < str2.length())
		{
			return "";
		}
		else if (str1.length() == str2.length())
		{
			if (str1.compare(str2) < 0)
			{
				return "";
			}
			else if (str1.compare(str2) == 0)
			{
				return "1";
			}
		}

		string result;
 		size_t str1_length = str1.length();
 		size_t str2_length = str2.length();
// 		for (size_t i = str2_length; i <= str1_length; i++)
// 		{
// 			string temp, temp_pre;
// 			temp = Sub(str1.substr(0, i), str2);
// 			if (temp.empty())
// 			{
// 				continue;
// 			}
// 			int cnt = 0;
// 			while (!temp.empty())
// 			{	
// 				cnt++;
// 				temp_pre = temp;
// 				temp = Sub(temp, str2);
// 			}
// 			str1.replace(0, i, temp_pre);
// 			string str_temp = "1";
// 			for (int r_bit = 0; r_bit < str1_length-i; r_bit++)
// 			{
// 				str_temp = Mult(str_temp, "10");
// 			}
// 			char chr_temp[32];
// 			sprintf_s(chr_temp, 32, "%d", cnt);
// 			str_temp = Mult(str_temp, chr_temp);
// 			result = Sum(result, str_temp);
// 		}

		//////////////////////////////////////////////////////////////////////////
		string temp, temp_pre;
		size_t delta = 0;
		for (; delta <= str1_length - str2_length; delta++)
		{
			temp = Sub(str1.substr(0, str2_length+delta), str2);
			if (!temp.empty())
			{
				break;
			}
		}
		
		int cnt = 0;
		while (!temp.empty())
		{	
			cnt++;
			temp_pre = temp;
			temp = Sub(temp, str2);
		}
		str1.replace(0, str2_length+delta, temp_pre);
		string str_temp = "1";
		for (size_t r_bit = 0; r_bit < str1_length-str2_length-delta; r_bit++)
		{
			str_temp = Mult(str_temp, "10");
		}
		char chr_temp[32];
		sprintf(chr_temp, "%d", cnt);
		str_temp = Mult(str_temp, chr_temp);
		result = Sum(Division(str1, str2), str_temp);
		//////////////////////////////////////////////////////////////////////////
		
		found = result.find_first_not_of('0');
		if (found != string::npos)
			result = result.substr(found, result.length()-found);
		else
		{
			if (result.length() > 1)
			{
				found = result.find_first_of('0');
				if (found != string::npos)
				{
					result = "0";
				}
			}
		}
		return result;
	}
	static bool Ascending(LotteryInfo a, LotteryInfo b)
	{
		string a_odds = a.Odds;
		string b_odds = b.Odds;
		if (a_odds.length() < b_odds.length())
		{
			return true;
		}
		else if (a_odds.length() > b_odds.length())
		{
			return false;
		}
		else
		{
			if (a_odds.compare(b_odds) < 0)
			{
				return true;
			}
			else if (a_odds.compare(b_odds) == 0)
			{
				string a_name = a.Name;
				string b_name = b.Name;
				if (a.Name.compare(b_name) < 0)
				{
					return true;
				}
				else 
					return false; 
			}
			else
			{
				return false;
			}
		}
	}	
	LotteryInfo getLotteryInfo(string rule)
	{	
		LotteryInfo lottery;
		size_t found1, found2;
		found1 = rule.find_first_of(":");
		if (found1 != string::npos)
			lottery.Name = rule.substr(0, found1);
		found1 += 2;
		found2 = rule.find(" ", found1);
		if (found1 != string::npos && found2 != string::npos)
		{
			lottery.Chocies = atoi(rule.substr(found1, found2-found1).c_str());
		}
		found1 = found2+1;
		found2 = rule.find(" ", found1);
		if (found1 != string::npos && found2 != string::npos)
		{
			lottery.Blanks = atoi(rule.substr(found1, found2-found1).c_str());
		}
		found1 = found2+1;
		found2 = rule.find(" ", found1);
		if (found1 != string::npos && found2 != string::npos)
		{
			if (rule.substr(found1, found2-found1) == "T")
			{
				lottery.Sorted = true;
			}
			else
			{
				lottery.Sorted = false;
			}
		}

		found1 = found2+1;
		if (found1 != string::npos)
		{
			if (rule.substr(found1, 1) == "T")
			{
				lottery.Unique = true;
			}
			else
			{
				lottery.Unique = false;
			}
		}
		return lottery;
	}
// 	unsigned long Factorial(int n)
// 	{
// 		unsigned long factorial = 1;
// 		while (n != 1)
// 		{
// 			factorial *= n;
// 			n--;
// 		}
// 		return factorial;
// 	}
	string Factorial(int n)
	{
		if (n <= 1)
		{
			return "1";
		}
		char temp[32];
		sprintf(temp, "%d", n);
		string str_n(temp);
		return Mult(str_n, Factorial(n-1));
	}
	string Combination(int n, int m)
	{
		//return Factorial(n) / (Factorial(n-m)*Factorial(m)); //it may overflow
		//return Arrangement(n, m) / Factorial(m);
		
		string str1 = Arrangement(n, m);
		string str2 = Factorial(m);
		return Division(str1, str2);
	}
	string Arrangement(int n, int m)
	{
		//return Factorial(n) / Factorial(n-m); ////it may overflow
		string result = "1";
		char temp[32];
		for (int i = 0; i < m; i++)
		{
			sprintf(temp, "%d", n-i);
			string str(temp);
			result =  Mult(result, str);
		}
		return result;
	}
	int fun(int n, int m)
	{
		if (n == m || n == 1)
		{
			return 1;
		}
		else if (n+1 == m)
		{
			return n;
		}
		else
		{
			int result = 0;
			for (int i = n-1; i <= m-1; i++)
			{
				result += fun(n-1, i);
			}
			return result;
		}
	}
	void calculateOdds(LotteryInfo &lottery)
	{
		if (lottery.Sorted)
		{
			string tickets = Combination(lottery.Chocies, lottery.Blanks);

			if (!lottery.Unique)
			{
				int nBlanks = lottery.Blanks-1;
				while (nBlanks > 0)
				{
					int nCoef = fun(nBlanks, lottery.Blanks);
					char temp[32];
					sprintf(temp, "%d", nCoef);
					string ceof(temp);
					string strF = Combination(lottery.Chocies, nBlanks);
					strF = Mult(strF, ceof);
					tickets = Sum(strF, tickets);
					nBlanks--;
				}
			}
			lottery.Odds = tickets;
		}
 		else if (!lottery.Sorted && lottery.Unique)
 		{
 			string tickets = Arrangement(lottery.Chocies, lottery.Blanks);
 			lottery.Odds = tickets;
 		}
 		else
 		{
			char temp[32];
			sprintf(temp, "%d", lottery.Chocies);
			string chocies(temp);
 			string tickets = "1";
			for (int i = 0; i < lottery.Blanks; i++)
			{
				tickets = Mult(tickets, chocies);
			}
 			lottery.Odds = tickets;
 		}
	}
public:
	vector <string> sortByOdds(vector <string> rules)
	{
		vector <string> result;
		vector <LotteryInfo> lottery_statis;
		vector <string>::iterator itor = rules.begin();
		for (; itor != rules.end(); itor++)
		{
			LotteryInfo lottery_info = getLotteryInfo(*itor);
			try
			{
				calculateOdds(lottery_info);
			}
			catch (exception& e)
			{
				cout << e.what() << endl;
			}
			
			lottery_statis.push_back(lottery_info);
		}
		sort(lottery_statis.begin(), lottery_statis.end(), Ascending);
		vector<LotteryInfo>::iterator itor_lottery = lottery_statis.begin();
		for (; itor_lottery != lottery_statis.end(); itor_lottery++)
		{
			result.push_back((*itor_lottery).Name);
		}
		return result;
	}
};


你可能感兴趣的:(TopCoder 2 Lottery)