C ++中的同义词句子
假设我们有一对等价的同义词和一个句子文本的对,我们必须找到所有可能的同义句子,它们按字典顺序排序。
因此,如果输入像同义词=[[“”happy“,”joy“],[”sad“,”sorrow“],[”joy“,”cheerful“]]]和text=”我今天很快乐,但昨天会感到悲伤”,那么输出将是““我今天很快乐,但昨天感到悲伤”,“我今天很快乐,但昨天感到悲伤”,“我今天很高兴但昨天感到悲伤”,“我今天很高兴但昨天感到悲伤”,“今天我很快乐,但昨天却难过”,“今天我很快乐,但昨天却很悲伤”]
为了解决这个问题,我们将遵循以下步骤-
定义映射的父对象,颜色和groupByColor
定义一个函数find(),需要s,
如果parent[s]与s相同,则-
父母[s]:=查找(父母[s])
返回父母
定义一个函数unionNode(),它将花费a,b,
x:=查找(a),y:=查找(b)
如果x与y相同,则-
父母[x]:=y
定义一个数组ans
定义一个函数getString(),这将花费t,
定义阵列温度
结束:=0
curr:=空字符串
对于end<t的大小,更新(将end增加1),请执行-
在临时结束时插入curr
curr:=空字符串
忽略以下部分,跳至下一个迭代
如果t[end]与空白空间相同,则-
curr:=curr并置t[end]
在临时结束时插入curr
返回温度
定义一个函数dfs(),它将接受字符串,idx,临时用空白字符串初始化它,
如果idx与字符串的大小相同,则-
在ans的末尾插入temp
返回
当前:=字符串[idx]
如果电流不是彩色的,则-
dfs(strings,idx+1,temp+currentconcatenate((如果idx+1与字符串的大小相同,则为空白字符串,否则为空格)))
除此以外
dfs(strings,idx+1,temp+z+((如果idx+1与字符串的大小相同,则为空白字符串,否则为空格)))
(将z增加1)
定义一组x=groupByColor[color[current]]
对于x中的每个元素z,执行-
定义功能seeGroups()
对于groupByColor中的每个元素,执行-
(将i增加1)
x:=i的秒
定义一套
对于x中的每个元素z-
定义一个函数generateSentences(),它将使用一个2D数组s,另一个字符串t,
n:=s的大小
对于初始化i:=0,当i<n时,更新(将i增加1),执行-
如果y不在父母中,则-
unionNode(x,y)
x:=s[i,0]
y:=s[i,1]
如果x不在父级中,则-
c:=1
对于初始化i:=0,当i<n时,更新(将i增加1),执行-
将x插入groupByColor[color[x]]
将z插入groupByColor[color[x]]
定义一组SS
将x插入ss
将y插入ss
groupByColor[color[x]]:=ss
颜色[y]:=c
(将c增加1)
x:=s[i,0]
z:=s[i,1]
y:=查找(x)
如果y不是彩色的,则-
颜色[x]:=颜色[y]
颜色[z]:=颜色[y]
如果color[x]不在groupByColor中,则-
除此以外
定义一个数组字符串=getString(t)
dfs(字符串,0)
排序数组ans
返回ans
例
让我们看下面的实现以更好地理解-
#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto< v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
public:
   map <string, string> parent;
   map <string, int> color;
   map <int, set<string<> groupByColor;
   string find(string s){
      if(parent[s] == s)return s;
         parent[s] = find(parent[s]);
      return parent[s];
   }
   void unionNode(string a, string b){
      string x = find(a);
      string y = find(b);
      if(x == y)return;
         parent[x] = y;
   }
   vector <string< ans;
   vector <string< getString(string t){
      vector <string< temp;
      int end = 0;
      string curr = "";
      for(;end < t.size(); end++){
         if(t[end] == ' '){
            temp.push_back(curr);
            curr = "";
            continue;
         }
         curr += t[end];
      }
      temp.push_back(curr);
      return temp;
   }
   void dfs(vector <string< &strings, int idx, string temp = ""){
      if(idx == strings.size()){
         ans.push_back(temp);
         return;
      }
      string current = strings[idx];
      if(color.find(current) == color.end()){
         dfs(strings, idx + 1, temp + current + (idx+1 == strings.size()?"":" "));
      }
      else{
         set <string< x = groupByColor[color[current]];
         set <string< :: iterator z = x.begin();
         while(z != x.end()){
            dfs(strings, idx + 1, temp + *z + (idx+1 == strings.size()?"":" "));
            z++;
         }
      }
   }
   void seeGroups(){
      map <int, set <string< > :: iterator i = groupByColor.begin();
      while(i != groupByColor.end()){
         set <string< x = i->second;
         set <string< :: iterator z = x.begin();
         while(z != x.end()){
            z++;
         }
         cout << endl;
         i++;
      }
   }
   vector<string< generateSentences(vector<vector<string<>& s, string t) {
      int n = s.size();
      for(int i = 0; i < n; i++){
         string x = s[i][0];
         string y = s[i][1];
         if(parent.find(x) == parent.end())parent[x] = x;
            if(parent.find(y) == parent.end())parent[y] = y;
               unionNode(x,y);
      }
      int c = 1;
      for(int i = 0; i < n; i++){
         string x = s[i][0];
         string z = s[i][1];
         string y = find(x);
         if(color.find(y) == color.end()){
            color[y] = c;
            c++;
         }
         color[x] = color[y];
         color[z] = color[y];
         if(groupByColor.find(color[x]) == groupByColor.end()){
            set <string< ss;
            ss.insert(x);
            ss.insert(y);
            groupByColor[color[x]] = ss;
         }
         else{
            groupByColor[color[x]].insert(x);
            groupByColor[color[x]].insert(z);
         }
      }
      vector <string< strings = getString(t);
      dfs(strings, 0);
      sort(ans.begin(), ans.end());
      return ans;
   }
};
main(){
   Solution ob;
   vector<vector<string<> v = {{"happy","joy"},{"sad","sorrow"},{"joy","cheerful"}};
   print_vector(ob.generateSentences(v, "I am happy today but was sad yesterday"));
}输入值
[["happy","joy"],["sad","sorrow"],["joy","cheerful"]] "I am happy today but was sad yesterday"
输出结果
[I am cheerful today but was sad yesterday, I am cheerful today but was sorrow yesterday, I am happy today but was sad yesterday, I am happy today but was sorrow yesterday, I am joy today but was sad yesterday, I am joy today but was sorrow yesterday, ]
