Satisfiability of Equality Equations - LeetCode

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Satisfiability of Equality Equations - LeetCode相关的知识,希望对你有一定的参考价值。

?????????string   pos   https   als   code   .com   ?????????   ??????   str   

????????????

Satisfiability of Equality Equations - LeetCode

?????????

  • ??????????????????pre

??????

????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????false??????????????????O(nm)

class Solution {
public:
    map<char,char> pre;
    char Find(char x)
    {
        char r = x;
        while(pre[r] != r)
        {
            r = pre[r];
        }
        return r;
    }
    void Join(char x,char y)
    {
        char fx = Find(x);
        char fy = Find(y);
        if(fx != fy) pre[fx] = fy;
    }
    void Init()
    {
        char letter[26] ={'a','b','c','d','e','f','g','h','i',
                          'j','k','l','m','n','o','p','q','r',
                          's','t','u','v','w','x','y','z'};
        for (auto l:letter)
        {
            pre[l] = l;
        }
        
    }
    bool equationsPossible(vector<string>& equations) {
        Init();
        for(auto e:equations)
        {
            if(e[1] == '=') Join(e[0],e[3]);
        }
        for(auto e:equations)
        {
            if(e[1] == '!' && Find(e[0]) == Find(e[3]))
            {
                return false;
            }
        }
        return true;
    }
};

????????????

??????

以上是关于Satisfiability of Equality Equations - LeetCode的主要内容,如果未能解决你的问题,请参考以下文章

Satisfiability

Satisfiability

6月8日

关于实用可满足性模理论(SMT)求解 Practical Satisfiability Modulo Theories (SMT) Solving...

LeetCode 990. 等式方程的可满足性 | Python

LeetCode 990. 等式方程的可满足性 | Python