LeetCode | 734. Sentence Similarity

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

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

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

However, similarity is 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. Forexample, the sentences words1 =["great"], words2 = ["great"], pairs = [] aresimilar, even though there are no specified similar word pairs.

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

Note:

· The length of words1 and words2 willnot exceed 1000.

· The length of pairs will notexceed 2000.

· The length of each pairs[i] willbe 2.

· The length of each words[i] and pairs[i][j] willbe in the range [1, 20].

简单题,判断两个string数组里面的对应位置上的string是否相等,在判断的时候需要考论pairs,

通过做这道题我学会了

在比较if(1<pairs.size()-1)的时候,如果pairs.size()-1<0这个条件语句结果会为true,所以以后在判断int类型和size_t类型的时候记得在前面加上(int)!比如:if(1<(int)(pairs.size()-1))

class Solution {

public:

   #define REP(i,s,t) for(int i=s;i<=t;i++)

   bool match(string a, string b)

{

      if(a.size() != b.size()) return false;

 

      REP(i,0, a.size() - 1)

            if(a[i] != b[i])

                  returnfalse;

 

      returntrue;

}

boolareSentencesSimilar(vector<string>& words1, vector<string>&words2, vector<pair<string, string>> pairs) {

      if(words1.size() != words2.size()) return false;

 

      for(inti=0;i<=(int)(words1.size() - 1);i++)

      {

            if(match(words1[i], words2[i])) continue;

            intj;

 

            intnum = pairs.size()-1;

            for(j=0;j<=num;j++)

            {

                  if(match(words1[i], pairs[j].first) && (match(words2[i], pairs[j].second)))goto next;

                  if(match(words1[i], pairs[j].second) && (match(words2[i],pairs[j].first))) goto next;

            }

            if(j > num) return false;

      next:;

      }

      returntrue;

}

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值