LeetCode 737. Sentence Similarity II

Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar.

For example, words1 = ["great", "acting", "skills"] and words2 = ["fine", "drama", "talent"] are similar, if the similar word pairs arepairs = [["great", "good"], ["fine", "good"], ["acting","drama"], ["skills","talent"]].

Note that the similarity relation is transitive. For example, if "great" and "good" are similar, and "fine" and "good" are similar, then "great" and "fine" are similar.

Similarity is also symmetric. For example, "great" and "fine" being similar is the same as "fine" and "great" being similar.

Also, a word is always similar with itself. For example, the sentences words1 = ["great"], words2 = ["great"], pairs = [] are similar, even though there are no specified similar word pairs.

Finally, sentences can only be similar if they have the same number of words. So a sentence like words1 = ["great"] can never be similar towords2 = ["doubleplus","good"].


   

这道题题目大意是给定一个相似的单词对序列, 并且这种相似性有传递性(这句话是个很明显的提示, 需要进行合并操作)。 然后给出两个单词向量, 问是否这两个向量每对都是相似的。

基本思路: 根据相似单词对列表建立并查集,建立完成之后扫描两个单词向量, 看每一对是否都在一个相似域中。(即每一对单词的根是否一致)

这里有个小trick : 按照题目直接进行并查集建立, 需要一个map[string, string].但是, 如果预先把单词利用一个map[string, int]做一个映射, 那么 我们需要处理的又是一个最简单的并查集了。

struct UF {
    UF(int n) {
        father.resize(n + 1);
        for (int i = 0; i < father.size(); ++i) {
            father[i] = i;
        }
    }
    bool Union(int a, int b) {
        int root1 = Find(a), root2 = Find(b);
        if (root1 != root2) {
            father[root2] = root1; //or father[root1] = root2;
            return true;
        }
        return false;
    }
    int Find(int k) {
        int i = k, j = k;
        while (i != father[i]) i = father[i]; // i is the root of k.
        //path comdense (to be continued)

        while (j != father[j]) {
            int tmp = father[j];
            father[j] = i;
            j = tmp;
        }
        return i;
    }
    vector<int> father;
};

class Solution {
public:
    bool areSentencesSimilarTwo(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
        if (words1.size() != words2.size()) return false;
        unordered_map<string, int> index;
        int count = 0;
        for (int i = 0; i < pairs.size(); ++i) {
            if (index[pairs[i].first] == 0) {
                ++count;
                index[pairs[i].first] = count;
            }
            if (index[pairs[i].second] == 0) {
                ++count;
                index[pairs[i].second] = count;
            }
        }

        UF uf_set(count);
        for (int i = 0; i < pairs.size(); ++i) {
            uf_set.Union(index[pairs[i].first], index[pairs[i].second]);
        }
        for (int i = 0; i < words1.size(); ++i) {
            if (words1[i] == words2[i]) continue;
            int root1 = uf_set.Find(index[words1[i]]), root2 = uf_set.Find(index[words2[i]]);
            if (root1 == 0 || root2 == 0 || root1 != root2) return false;
        }
        return true;
    }
};


              
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值