-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1258-Synonymous Sentences.cpp
54 lines (54 loc) · 1.54 KB
/
1258-Synonymous Sentences.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public:
unordered_set<string> visited;
unordered_map<string,vector<string>> adjList;
vector<string> dfs(string &s)
{
visited.insert(s);
vector<string> result,temp;
result.push_back(s);
for(string &next:adjList[s])
{
if(!visited.count(next))
temp=dfs(next);
result.insert(result.end(),temp.begin(),temp.end());
}
return result;
}
vector<string> result;
string temp;
void create(string &s,int i)
{
if(i>=s.length())
{
temp.pop_back();
result.push_back(temp);
temp+=' ';
return;
}
string word=s.substr(i,s.find(' ',i)-i);
int tmpbegin=temp.length();
vector<string> tempList;
if(adjList.count(word))
{
visited.clear();
tempList=dfs(word);
sort(tempList.begin(),tempList.end());
for(string &next:tempList)
{
temp+=next+' ';
create(s,i+word.length()+1);
temp.erase(tmpbegin,temp.length()-tmpbegin);
}
}
else
temp+=word+' ',create(s,i+word.length()+1),temp.erase(tmpbegin,temp.length()-tmpbegin);
}
vector<string> generateSentences(vector<vector<string>>& synonyms, string text)
{
for(vector<string> &v:synonyms)
adjList[v.front()].push_back(v.back()),adjList[v.back()].push_back(v.front());
create(text,0);
return result;
}
};