Advertisement
nikunjsoni

843

Jun 27th, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. /**
  2.  * // This is the Master's API interface.
  3.  * // You should not implement it, or speculate about its implementation
  4.  * class Master {
  5.  *   public:
  6.  *     int guess(string word);
  7.  * };
  8.  */
  9. class Solution {
  10. public:
  11.  
  12.     void findSecretWord(vector<string>& wordlist, Master& master) {
  13.         for(int i = 0, x = 0; i < 10 && x < 6; ++i) {
  14.             unordered_map<string, int> count;
  15.             for (string w1 : wordlist)
  16.                 for (string w2 : wordlist)
  17.                     if (match(w1, w2) == 0)
  18.                         count[w1]++;
  19.             pair<string, int> minimax = {wordlist[0], 1000};
  20.             for (string w : wordlist)
  21.                 if (count[w] <= minimax.second)
  22.                     minimax = make_pair(w, count[w]);
  23.             x = master.guess(minimax.first);
  24.             vector<string> wordlist2;
  25.             for (string w : wordlist)
  26.                 if (match(minimax.first, w) == x)
  27.                     wordlist2.push_back(w);
  28.             wordlist = wordlist2;
  29.         }
  30.     }
  31.     int match(string a, string b) {
  32.         int matches = 0;
  33.         for (int i = 0; i < a.length(); ++i)
  34.             if (a[i] == b[i])
  35.                 matches++;
  36.         return matches;
  37.     }
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement