Advertisement
den4ik2003

Untitled

Aug 27th, 2023
1,284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <unordered_set>
  6.  
  7. int SuitToNum(char suit) {
  8.   if (suit == 'S') return 0;
  9.   if (suit == 'C') return 1;
  10.   if (suit == 'D') return 2;
  11.   return 3;
  12. }
  13.  
  14. char NumToSuit(int num) {
  15.   if (num == 0) return 'S';
  16.   if (num == 1) return 'C';
  17.   if (num == 2) return 'D';
  18.   return 'H';
  19. }
  20.  
  21. int ValToNum(char val) {
  22.   if (val <= '9' && val >= '2') {
  23.     return val - '2';
  24.   }
  25.   if (val == 'T') return 8;
  26.   if (val == 'J') return 9;
  27.   if (val == 'Q') return 10;
  28.   if (val == 'K') return 11;
  29.   return 12;
  30. }
  31.  
  32. std::string NumToVal(int num) {
  33.   if (num >= 0 && num <= 7) return std::to_string(num + 2);
  34.   if (num == 8) return "T";
  35.   if (num == 9) return "J";
  36.   if (num == 10) return "Q";
  37.   if (num == 11) return "K";
  38.   return "A";
  39. }
  40.  
  41. std::pair<int, int> Input(std::vector<std::vector<bool>>& used,
  42.                           std::vector<std::pair<std::string, std::string>>& cards,
  43.                           int n) {
  44.   std::string myFirstCard, mySecondCard;
  45.   std::cin >> myFirstCard >> mySecondCard;
  46.   used[SuitToNum(myFirstCard[1])][SuitToNum(myFirstCard[0])] = true;
  47.   used[SuitToNum(mySecondCard[1])][SuitToNum(mySecondCard[0])] = true;
  48.   cards[0] = {myFirstCard, mySecondCard};
  49.  
  50.   for (int i = 1; i < n; ++i) {
  51.     std::string firstCard, secondCard;
  52.     std::cin >> firstCard >> secondCard;
  53.     used[SuitToNum(firstCard[1])][SuitToNum(firstCard[0])] = true;
  54.     used[SuitToNum(secondCard[1])][SuitToNum(secondCard[0])] = true;
  55.     cards[i] = {firstCard, secondCard};
  56.   }
  57.   return {ValToNum(myFirstCard[0]), ValToNum(mySecondCard[0])};
  58. }
  59.  
  60. std::vector<std::string> SameCardsCase(std::vector<std::vector<bool>> used,
  61.                                        std::vector<std::pair<std::string, std::string>>& cards,
  62.                                        int val, int n) {
  63.   std::vector<std::string> answer;
  64.   std::vector<char> suits = {'S', 'C', 'D', 'H'};
  65.   // set case
  66.   for (char suit : suits) {
  67.     if (!used[SuitToNum(suit)][val]) {
  68.       std::string cval(NumToVal(val));
  69.       cval += suit;
  70.       answer.push_back(cval);
  71.     }
  72.   }
  73.   // pair case
  74.   bool isImpossible = false;
  75.   std::vector<int> withSame; // кому нельзя дать сет
  76.   for (int i = 1; i < n; ++i) {
  77.     char val1 = cards[i].first[0];
  78.     char val2 = cards[i].first[1];
  79.     int numVal1 = ValToNum(val1);
  80.     int numVal2 = ValToNum(val2);
  81.     if (val1 == val2) { // нельзя, тк сет
  82.       if (numVal1 > val) {
  83.         isImpossible = true;
  84.         break;
  85.       }
  86.       for (char suit : suits) {
  87.         used[SuitToNum(suit)][val1] = true;
  88.       }
  89.     }
  90.     int numValMax = std::max(numVal1, numVal2);
  91.     if (numValMax > val) { // нельзя, тк будет пара сильнее моей
  92.       for (char suit : suits) {
  93.         used[SuitToNum(suit)][val] = true;
  94.       }
  95.     }
  96.   }
  97.   if (isImpossible) return answer;
  98.   for (int i = 0; i < 4; ++i) {
  99.     for (int j = 0; j < 13; ++j) {
  100.       if (!used[i][j]) {
  101.         std::string cval(NumToVal(j));
  102.         cval += NumToSuit(i);
  103.         answer.push_back(cval);
  104.       }
  105.     }
  106.   }
  107.   return answer;
  108. }
  109.  
  110. int main() {
  111.   int t;
  112.   std::cin >> t;
  113.   for (int iter = 0; iter < t; ++iter) {
  114.     int n;
  115.     std::cin >> n;
  116.     std::vector<std::vector<bool>> used(4, std::vector<bool>(13));
  117.     std::vector<std::pair<std::string, std::string>> cards(n);
  118.     std::pair<int, int> myCards;
  119.     myCards = Input(used, cards, n);
  120.  
  121.     if (myCards.first == myCards.second) { // 2 одинаковые
  122.       auto answer = SameCardsCase(used, cards, myCards.first, n);
  123.       std::cout << answer.size() << "\n";
  124.       for (auto& el : answer) {
  125.         std::cout << el << "\n";
  126.       }
  127.     }
  128.  
  129.  
  130.   }
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement