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 andblanks) 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 andchoices, 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. Ifsorted is set to true, then the numbers on your ticket must be written in non-descending order. Ifsorted is set to false, then the numbers may be written in any order. Likewise, ifunique is set to true, then each number you write on your ticket must be distinct. Ifunique is set to false, then repeats are allowed. Here are some example lottery tickets, where choices = 15 and blanks = 4:
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 | |||||||||||||
| |||||||||||||
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) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
|
以上是题目,其算法核心是计算彩票在各种规则下(由后两个参数的限制,形成4中条件)的排列组合数。sorted = F unique = F条件下,显然有pow(choice, blank)种组合。其他几种情况应该这样考虑:
1. sorted = T unique = T条件下,相当于从已经排列好的choice个数中取出blank个,并保持相对位置不变,作为彩票号码,因此有binomial(choice, blank)种;
2. sorted = T unique = F条件下,相当于从已经排列好的choice个数中取出blank个,可重复取,相对位置保持不变,根据多重集合的性质,因此有binomial(choice+blank-1, blank)中;
3. sorted = F unique = T条件下,相当于在1的基础上,将取出的blank个数字打乱顺序,因此有binomial(choice, blank) * blank! 种。
参考知识:
http://en.wikipedia.org/wiki/Binomial_coefficient
http://en.wikipedia.org/wiki/Multiset