地里新农-请到考试中心学习规则
- 积分
- 1
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2022-3-8
- 最后登录
- 1970-1-1
|
解决方案- #include <iostream>
- #include <string>
- #include <unordered_map>
- #include <vector>
- #include <queue>
- #include <algorithm>
- using namespace std;
- /**
- *
- * TEST
- *
- * */
- class Test{
- public:
- static int totalCase;
- static int testCase;
- Test(bool expected, bool result);
- Test(vector<string> expected, vector<string> result);
- void printVector(vector<string> v);
- static void printTestSuit(); // to directly access Test::printTestSuit() in main
- };
- int Test::totalCase = 0;
- int Test::testCase = 0;
- Test::Test(bool expected, bool result){
- totalCase++;
- if(result == expected){
- cout << "Test Passed : " << "(expected: " << expected << ", result: " << result << ")" << endl;
- testCase++;
- }
- else {
- cout << "Test Failed : " << "(expected: " << expected << ", result: " << result << ")" << endl;
- }
- }
- Test::Test(vector<string> expected, vector<string> result){
- totalCase++;
- if(expected == result){
- cout << "Test Passed : " << "(expected: "; printVector(expected); cout << ", result: "; printVector(result); cout << ")" << endl;
- testCase++;
- } else {
- cout << "Test Failed : " << "(expected: "; printVector(expected); cout << ", result: "; printVector(result); cout << ")" << endl;
- }
- }
- void Test::printVector(vector<string> v){
- cout << "{";
- for(int i = 0; i < v.size() - 1; i++){
- cout << v[i] << ", ";
- }
- cout << v[v.size() - 1] << "}";
- }
- void Test::printTestSuit(){
- cout << testCase << " / " << totalCase << " Total Cases Passed" << endl;
- }
- /**
- *
- * WISHLIST
- *
- * */
- class WishList{
- public:
- unordered_map<string, vector<string>> wishList = {
- {"a", {"c", "d"}},
- {"b", {"d", "a", "c"}},
- {"c", {"a", "b"}},
- {"d", {"c", "a", "b"}},
- {"e", {""}}
- // {"a", {"b", "c", "d"}},
- // {"b", {"a", "d", "c"}},
- // {"c", {"d" ,"a", "b"}},
- // {"d", {"a", "c"}}
- // {"a", {"c", "d"}},
- // {"b", {"d", "a", "c"}},
- // {"c", {"a", "b"}},
- // {"d", {"c", "a", "b"}}
-
- };
- bool has_mutual_first_choice(string userName);
- bool has_mutual_pair_for_rank(string userName, int rank);
- bool has_mutual_pair_for_rank(string userName, int uRank, int mRank);
- vector<string> changePair(string user, int index);
- vector<string> changed_pairings(string userName, int index);
- };
- // part 1
- bool WishList::has_mutual_first_choice(string userName){
- if(!wishList[userName].empty() && wishList.find(userName) != wishList.end()){
- const int firstChoice = 0;
- string usersFirstChoice = wishList[userName][firstChoice];
- if(wishList.find(usersFirstChoice) != wishList.end()
- && !wishList[usersFirstChoice].empty()
- && wishList[usersFirstChoice][firstChoice] == userName){
- return true;
- }
- }
- return false;
- }
- // part 2
- bool WishList::has_mutual_pair_for_rank(string userName, int rank){
- if(wishList[userName].size() > rank && wishList.find(userName) != wishList.end()){
- string usersChoice = wishList[userName][rank];
- if(wishList.find(usersChoice) != wishList.end()
- && wishList[usersChoice].size() > rank
- && wishList[usersChoice][rank] == userName)
- return true;
- }
- return false;
- }
- bool WishList::has_mutual_pair_for_rank(string user, int uRank, int mRank){
- if(uRank < 0) return false;
- if(wishList[user].size() > uRank && wishList.find(user) != wishList.end()){
- string mutualUser = wishList[user][uRank];
- if(wishList.find(mutualUser) != wishList.end()
- && wishList[mutualUser].size() > mRank
- && wishList[mutualUser][mRank] == user)
- return true;
- }
- return false;
- }
- // part 3-1 - swap user rank index with index-1
- vector<string> WishList::changePair(string user, int index){
- // return matched all users before and after swap with same mutual ranking
- vector<string> ans;
- // before swap
- if(wishList[user].size() > index && wishList.find(user) != wishList.end()){
- vector<string> usersWishlist {wishList[user]};
- for(int i = 0 ; i < usersWishlist.size(); i++){
- if(has_mutual_pair_for_rank(user, i))
- ans.push_back(usersWishlist[i]);
- }
- // after swap
- if(index > 0){
- if(has_mutual_pair_for_rank(user, index, index - 1))
- ans.push_back(usersWishlist[index]);
- if(has_mutual_pair_for_rank(user, index - 1, index))
- ans.push_back(usersWishlist[index - 1]);
- }
- }
- return ans;
- }
- vector<string> WishList::changed_pairings(string userName, int index){
- // before swap
- vector<string> ans;
- if(wishList[userName].size() > index && wishList.find(userName) != wishList.end()){
- vector<string> usersWishList {wishList[userName]};
- if(index > 0){
- if(has_mutual_pair_for_rank(userName, index) != has_mutual_pair_for_rank(userName, index, index - 1))
- ans.push_back(usersWishList[index]);
- if(has_mutual_pair_for_rank(userName, index - 1) != has_mutual_pair_for_rank(userName, index - 1, index))
- ans.push_back(usersWishList[index - 1]);
- }
- }
- if(ans.empty()) return {""};
- return ans;
- }
- int main(){
- WishList* wishList = new WishList();
- Test(1, wishList->has_mutual_first_choice("a"));
- Test(1, wishList->has_mutual_first_choice("c"));
- Test(0, wishList->has_mutual_first_choice("b"));
- Test(1, wishList->has_mutual_pair_for_rank("a", 1));
- Test(1, wishList->has_mutual_pair_for_rank("a", 0));
- Test({"a","b","d"}, wishList->changePair("c", 1));
- Test({"a"}, wishList->changed_pairings("d", 1));
- Test({"c"}, wishList->changed_pairings("b", 2));
- Test({""}, wishList->changed_pairings("b", 1));
- Test::printTestSuit();
- return 0;
- }
复制代码 |
|