990. Satisfiability of Equality Equations

Given an array equations of strings that represent relationships between variables, each string equations[i] has length 4 and takes one of two different forms: "a==b" or "a!=b".  Here, a and b are lowercase letters (not necessarily different) that represent one-letter variable names.

Return true if and only if it is possible to assign integers to variable names so as to satisfy all the given equations.

 

Example 1:

Input: ["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: ["b==a","a==b"]
Output: true
Explanation: We could assign a = 1 and b = 1 to satisfy both equations.

Example 3:

Input: ["a==b","b==c","a==c"]
Output: true

Example 4:

Input: ["a==b","b!=c","c==a"]
Output: false

Example 5:

Input: ["c==c","b==d","x!=z"]
Output: true

 

Note:

  1. 1 <= equations.length <= 500
  2. equations[i].length == 4
  3. equations[i][0] and equations[i][3] are lowercase letters
  4. equations[i][1] is either '=' or '!'
  5. equations[i][2] is '='

思路:先根据等号建立union-find,然后对不等号进行find,如果find结果相同说明冲突

class Solution(object):
    def equationsPossible(self, equations):
        """
        :type equations: List[str]
        :rtype: bool
        """
        fa=[i for i in range(26)]
        base=ord('a')
        def find(x):
            while x!=fa[x]: x=fa[x]
            return x
        def union(i,j):
            ii,jj=find(i),find(j)
            fa[ii]=jj
        for eq in equations:
            if eq[1]=='=':
                union(ord(eq[0])-base, ord(eq[3])-base)
        
        for eq in equations:
            if eq[1]=='!':
                if find(ord(eq[0])-base)==find(ord(eq[3])-base):
                    return False
        return True
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
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、付费专栏及课程。

余额充值