topcoder算法练习2
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:
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) | |||||||||||||
|
总的来说此题不难,就是组合数学题
#include <vector> #include <string> #include <stdlib.h> #include <stdint.h> #include <stdio.h> using namespace std; class Lottery { public: vector <string> sortByOdds(vector <string>); void compileRule(string, string&, uint64_t&, uint64_t&, int&, int&); uint64_t computeOdd(uint64_t, uint64_t, int, int); vector<string> ascendOrder(uint64_t* ,string[], int); }; vector <string> Lottery::sortByOdds(vector <string> rules) { int len=rules.size(); int i; string names[len]; uint64_t choices[len]; uint64_t blanks[len]; uint64_t odds[len]; int sorted[len]; int unique[len]; vector<string> result; if(len==0){ return result; } for(i=0;i<len;i++){ compileRule(rules.at(i), names[i], choices[i], blanks[i], sorted[i], unique[i]); } for(i=0;i<len;i++){ odds[i] = computeOdd(choices[i], blanks[i], sorted[i], unique[i]); } result=ascendOrder(odds, names, len); return result; } void Lottery::compileRule(string rule, string& name, uint64_t& choice, uint64_t& blank, int& sorted, int&unique){ char *a = (char*)malloc(sizeof(char)); choice = 0; blank = 0; //extract name until find an : string::iterator iter; for(iter=rule.begin();*iter!=':';iter++){ *a=*iter; name.append((const char*)a); } iter=iter+2; //extract choice for(;*iter!=' ';iter++){ choice=choice*10+*iter-48; } iter++; //extract blank for(;*iter!=' ';iter++){ blank=blank*10+*iter-48; } //printf("blank %d\n",blank); iter++; if(*iter=='T') sorted=1; else sorted=0; iter=iter+2; if(*iter=='T') unique=1; else unique=0; //printf("choice %d, blank %d, sorted %d, unique %d\n",choice,blank, sorted, unique); } uint64_t Lottery::computeOdd(uint64_t choice, uint64_t blank, int sorted, int unique){ int i; uint64_t result=1; uint64_t tresult=1; if(sorted == 0 && unique == 0){ for(i = 0;i<blank;i++){ result=result*choice; } } if(sorted==1 && unique==0){ for(i = 0;i<blank;i++){ result=result*choice; } for(i=0;i<blank;i++){ tresult=tresult*(choice-i); } for(i=0;i<blank;i++){ tresult=tresult/(i+1); } result=result-tresult; } if(sorted==0 && unique==1){ for(i=0;i<blank;i++){ result=result*(choice-i); } } if(sorted==1 && unique==1){ for(i=0;i<blank;i++){ result=result*(choice-i); } for(i=0;i<blank;i++){ result=result/(i+1); } } printf("odd %d\n",result); //printf("choice %d, blank %d, sorted %d, unique %d, odd %d\n",choice,blank, sorted, unique,result); return result; } vector<string> Lottery::ascendOrder(uint64_t* odd, string* names, int len){ uint64_t temp; int order[len]; int i,j, torder; vector<string> result; for(i=0;i<len;i++) order[i]=i; for(i=0;i<len-1;i++){ for(j=0;j<len-1-i;j++){ if (odd[j]>odd[j+1]){ temp=odd[j]; odd[j]=odd[j+1]; odd[j+1]=temp; torder=order[j]; order[j]=order[j+1]; order[j+1]=torder; } } } for(i=0;i<len;i++) printf("%d ",odd[i]); for(i=0;i<len;i++) printf("%d ",order[i]); for(i=0;i<len;i++){ result.push_back(names[order[i]]); } return result; }