#990 Satisfiability of Equality Equations

Description

You are given an array of strings equations that represent relationships between variables where each string equations[i] is of length 4 and takes one of two different forms: “xi==yi” or “xi!=yi”.Here, xi and yi are lowercase letters (not necessarily different) that represent one-letter variable names.

Return true if it is possible to assign integers to variable names so as to satisfy all the given equations, or false otherwise.

Examples

Example 1:

Input: equations = [“a==b”,“b!=a”]
Output: false
Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second.
There is no way to assign the variables to satisfy both equations.

Example 2:

Input: equations = [“ba","ab”]
Output: true
Explanation: We could assign a = 1 and b = 1 to satisfy both equations.

Constraints:

1 <= equations.length <= 500
equations[i].length == 4
equations[i][0] is a lowercase letter.
equations[i][1] is either ‘=’ or ‘!’.
equations[i][2] is ‘=’.
equations[i][3] is a lowercase letter.

思路

感觉就是用并查集,判断两个单词是否在同一个簇中,要注意的是,需要先进行 “==” 的union,再进行 “!=” 的判断,因为没有操作来存储 “!=” 的约束

代码

class Solution {
    public void union(int[] parents, int x, int y) {
        int px = findParent(parents, x);
        int py = findParent(parents, y);
        
        if (px != py)
            parents[px] = py;
    }
    
    public int findParent(int[] parents, int x) {
        if(parents[x] != x)
            parents[x] = findParent(parents, parents[x]);
        return parents[x];
    }
    
    public boolean equationsPossible(String[] equations) {
        int[] parents = new int[26];
        for (int i = 0; i < parents.length; i++)
            parents[i] = i;
        
        for (String eq: equations) {
            if (eq.charAt(1) == '=')
                union(parents, eq.charAt(0) - 'a', eq.charAt(3) - 'a');
        }
        for (String eq: equations) {
            if (eq.charAt(1) == '!') {
                int x = findParent(parents, eq.charAt(0) - 'a');
                int y = findParent(parents, eq.charAt(3) - 'a');
                if (x == y)
                    return false;
            }
        }
        
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To use the ALC-Worlds algorithm, we first need to convert the Tbox and the concept name B0 into their ALC syntax: B0 ≡ B1 ⊓ B2 B1 ≡ ∃r.B3 B2 ≡ B4 ⊓ B5 B3 ≡ P B4 ≡ ∃r.B6 B5 ≡ B7 ⊓ B8 B6 ≡ Q B7 ≡ ∀r.B4 B8 ≡ ∀r.B9 B9 ≡ ∀r.B10 B10 ≡ ¬P Now, we can start the algorithm: 1. Initialize the set of worlds W to contain the empty interpretation {}. 2. For each concept name and role name, initialize its interpretation in each world to the empty set. 3. For each concept name A that appears in the Tbox, add the pair (A, {}) to the interpretation of A in each world. 4. For each role name r that appears in the Tbox, add the pair (r, {}) to the interpretation of r in each world. 5. For each concept name A that appears in the definition of a concept or role in the Tbox, propagate its interpretation to all worlds that satisfy that concept or role. 6. For each concept name A that appears in the definition of a concept or role in the Tbox, repeat step 5 until no more interpretations can be propagated. 7. For each world w in W, check if w satisfies B0. If so, return "satisfiable" and the model consisting of all interpretations in w. If not, mark w as unsatisfiable. 8. If all worlds are marked as unsatisfiable, return "unsatisfiable". Recursion tree for a successful run: ``` W={} | W1={r={}, B3={}} | W2={r={}, B3={}, B6={}} | W3={r={}, B3={}, B6={Q}} | W4={r={}, B3={P}, B6={Q}} | W5={r={r1:W4}, B3={P}, B6={Q}} | W6={r={r1:W4, r2:W5}, B3={P}, B6={Q}} | W7={r={r1:W4, r2:W5, r3:W6}, B3={P}, B6={Q}} | W8={r={r1:W4, r2:W5, r3:W6, r4:W7}, B3={P}, B6={Q}} | W9={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8}, B3={P}, B6={Q}} | W10={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9}, B3={P}, B6={Q}} | W11={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9}, B3={}, B6={Q}} | W12={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9}, B3={P}, B6={}} | W13={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9, r7:W12}, B3={P}, B6={}} | W14={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9, r7:W12, r8:W13}, B3={P}, B6={}} | W15={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9, r7:W12, r8:W13, r9:W14}, B3={P}, B6={}} | W16={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9, r7:W12, r8:W13, r9:W14, r10:W15}, B3={P}, B6={¬P}} The algorithm returns a positive result (i.e., "satisfiable") with the model consisting of all interpretations in W16. Recursion tree for an unsuccessful run: ``` W={} | W1={r={}, B3={}} | W2={r={}, B3={}, B6={}} | W3={r={}, B3={}, B6={Q}} | W4={r={}, B3={P}, B6={Q}} | W5={r={r1:W4}, B3={P}, B6={Q}} | W6={r={r1:W4, r2:W5}, B3={}, B6={Q}} | W7={r={r1:W4, r2:W5, r3:W6}, B3={P}, B6={Q}} | W8={r={r1:W4, r2:W5, r3:W6, r4:W7}, B3={P}, B6={Q}} | W9={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8}, B3={P}, B6={Q}} | W10={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9}, B3={}, B6={Q}} | W11={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9, r7:W10}, B3={P}, B6={Q}} | W12={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9, r7:W10, r8:W11}, B3={P}, B6={Q}} | W13={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9, r7:W10, r8:W11, r9:W12}, B3={P}, B6={Q}} | W14={r={r1:W4, r2:W5, r3:W6, r4:W7, r5:W8, r6:W9, r7:W10, r8:W11, r9:W12, r10:W13}, B3={P}, B6={¬P}} The algorithm returns a negative result (i.e., "unsatisfiable") as all worlds are marked as unsatisfiable.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值