동등한 단어 동의어와 문장 텍스트의 쌍 목록이 있다고 가정하고 사전순으로 정렬된 가능한 모든 동의어 문장을 찾아야 합니다.
따라서 입력이 동의어 =[["happy","joy"],["sad","sorrow"],["joy","cheerful"]] 및 text ="나는 오늘 행복하지만 어제 슬펐다"라고 하면 ["오늘은 즐거우나 어제는 슬펐다", "오늘은 즐거우나 어제는 슬펐다", "오늘은 기쁘지만 어제는 슬펐다", "오늘은 기쁘지만 어제는 슬펐다"가 출력된다. 어제는 슬펐다", "오늘은 기쁘지만 어제는 슬펐다", "오늘은 기쁘지만 어제는 슬펐다"]
이 문제를 해결하기 위해 다음 단계를 따릅니다. −
-
지도 부모, 색상 및 groupByColor 정의
-
find() 함수를 정의하면 s가 필요합니다.
-
부모[s]가 s와 같으면 -
-
부모[들] :=찾기(부모[들])
-
-
부모 반환[]
-
UnionNode() 함수를 정의하면, b,
-
x :=찾기(a), y :=찾기(b)
-
x가 y와 같으면 -
-
부모[x] :=y
-
-
배열 정의
-
getString() 함수를 정의하면 t가 필요합니다.
-
어레이 온도 정의
-
끝 :=0
-
curr :=빈 문자열
-
끝
-
t[end]가 빈 공간과 같으면 -
-
temp의 끝에 curr 삽입
-
curr :=빈 문자열
-
다음 부분은 무시하고 다음 반복으로 건너뜁니다.
-
-
curr :=curr 연결 t[end]
-
-
temp의 끝에 curr 삽입
-
반환 온도
-
dfs() 함수를 정의하면 문자열, idx, 임시가 빈 문자열로 초기화됩니다.
-
idx가 문자열의 크기와 같으면 -
-
ans의 끝에 temp 삽입
-
반환
-
-
현재 :=문자열[idx]
-
전류가 색상이 아닌 경우 -
-
dfs(strings, idx + 1, temp + current concatenate ((idx + 1 이 문자열의 크기와 같으면 공백 문자열, 그렇지 않으면 공백)))
-
-
그렇지 않으면
-
한 세트 정의 x =groupByColor[color[current]]
-
x의 각 요소 z에 대해 −
를 수행합니다.-
dfs(strings, idx + 1, temp + z + ((idx + 1이 문자열의 크기와 같으면 공백 문자열, 그렇지 않으면 공백)))
-
(z를 1씩 증가)
-
-
-
함수 정의 seeGroups()
-
groupByColor의 각 요소 i에 대해 다음을 수행합니다. -
-
x :=설정된 i의 초
-
한 세트 정의
-
x −
의 각 요소 z에 대해-
(i를 1씩 증가)
-
-
-
generateSentences() 함수를 정의합니다. 이것은 하나의 2D 배열 s, 다른 문자열 t,
를 취합니다. -
n :=s
의 크기 -
initialize i :=0의 경우, i
-
x :=s[i, 0]
-
y :=s[i, 1]
-
x가 부모에 없으면 -
-
y가 부모에 없으면 -
-
유니온노드(x, y)
-
-
-
-
c :=1
-
initialize i :=0의 경우, i
-
x :=s[i, 0]
-
z :=s[i, 1]
-
y :=찾기(x)
-
y가 색상이 아닌 경우 -
-
색상[y] :=c
-
(c를 1씩 증가)
-
-
색상[x] :=색상[y]
-
색상[z] :=색상[y]
-
color[x]가 groupByColor에 없으면 -
-
한 세트 ss 정의
-
ss에 x 삽입
-
ss에 y 삽입
-
groupByColor[색상[x]] :=ss
-
-
그렇지 않으면
-
x를 groupByColor[color[x]]
에 삽입 -
groupByColor[color[x]]
에 z를 삽입합니다.
-
-
-
배열 문자열 정의 =getString(t)
-
dfs(문자열, 0)
-
배열 정렬
-
반환
예시
이해를 돕기 위해 다음 구현을 살펴보겠습니다. −
#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, ]