Skip to content

843. Guess the Word #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
namespace-io opened this issue Jul 18, 2019 · 0 comments
Open

843. Guess the Word #325

namespace-io opened this issue Jul 18, 2019 · 0 comments

Comments

@namespace-io
Copy link
Owner

先选一个与字符串其他字符串match为0个数最少的,然后猜这个数,选到与猜出的值相匹配的字符串集合,重新进行再猜

/**
 * // This is the Master's API interface.
 * // You should not implement it, or speculate about its implementation
 * class Master {
 *   public:
 *     int guess(string word);
 * };
 */
class Solution {
public:
    int match(string &s1, string &s2){
        int r = 0;
        for(int i = 0; i < 6; i++){
            if(s1[i] == s2[i]) r++;
        }
        return r;
    }
    void findSecretWord(vector<string>& ws, Master& master) {
        vector<int> w;
        for(int i = 0; i < ws.size(); i++) w.push_back(i);
        for(int i = 0; i < 10; i++){
            unordered_map<int, int> m;
            for(int i = 0; i < w.size(); i++){
                for(int j = 0; j < w.size(); j++){
                    if(match(ws[w[i]], ws[w[j]]) == 0)
                        m[w[i]]++;
                }
            }
            int minCount = 1000;
            int minf = w[0];
            for(int i = 0; i < w.size(); i++){
                if(m[w[i]] < minCount){
                    minCount = m[w[i]];
                    minf = w[i];
                }
            }
            
            int x = master.guess(ws[minf]);
            if(x == 6) return;
            vector<int> r;
            for(int i = 0; i < w.size(); i++){
                if(match(ws[w[i]], ws[minf]) == x){
                    r.push_back(w[i]);
                }
            }
            
            w = r;
        }
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant