1、题目
https://leetcode-cn.com/problems/satisfiability-of-equality-equations/
2、题意
题解1:并查集
将a-z当做0-25表示;
将相等的合并为一个集合;
将不相等的判断时否为一个集合 即find(a)==find(b);相等则等式不成立
class Solution {
public:
vector<int> p;
int find(int x)
{
return x==p[x]?x:p[x] = find(p[x]);
}
bool equationsPossible(vector<string>& s) {
p = vector<int>(26,0);
for(int i=0;i<26;i++)
p[i] = i;
for(int i=0;i<s.size();i++)
{
int a = s[i][0]-'a',b = s[i][3]-'a';
if(s[i][1]=='=')
{
a = find(a),b = find(b);
if(a!=b)
p[a] = b;
}
}
for(int i=0;i<s.size();i++)
{
int a = s[i][0]-'a',b = s[i][3]-'a';
if(s[i][1]=='!')
if(find(a)==find(b))
return false;
}
return true;
}
};