|
| 1 | +#include "public.h" |
| 2 | + |
| 3 | +// |
| 4 | + |
| 5 | +class Solution { |
| 6 | +private: |
| 7 | + unordered_map<int, int> um; |
| 8 | + |
| 9 | + void init(vector<int>& vals) |
| 10 | + { |
| 11 | + for (auto& s : vals) |
| 12 | + { |
| 13 | + um[s] = s; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + int GetFather(int x) { |
| 18 | + return (um[x] == x ? x : um[x] = GetFather(um[x])); |
| 19 | + } |
| 20 | + |
| 21 | + void Merge(int x, int y) { |
| 22 | + int a = GetFather(x); |
| 23 | + int b = GetFather(y); |
| 24 | + if (a != b) |
| 25 | + { |
| 26 | + um[b] = a; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + vector<string> split(string t) |
| 31 | + { |
| 32 | + string pre = ""; |
| 33 | + vector<string> res; |
| 34 | + for (auto& ite : t) |
| 35 | + { |
| 36 | + if (ite == ' ') |
| 37 | + { |
| 38 | + res.push_back(pre); |
| 39 | + pre = ""; |
| 40 | + } |
| 41 | + else pre.push_back(ite); |
| 42 | + } |
| 43 | + res.push_back(pre); |
| 44 | + return res; |
| 45 | + } |
| 46 | + |
| 47 | + string strmerge(vector<string> input) |
| 48 | + { |
| 49 | + string pre; |
| 50 | + for (auto& ite : input) |
| 51 | + { |
| 52 | + pre += ite; |
| 53 | + pre += " "; |
| 54 | + } |
| 55 | + pre.pop_back(); |
| 56 | + return pre; |
| 57 | + } |
| 58 | + |
| 59 | +public: |
| 60 | + vector<string> generateSentences(vector<vector<string>>& synonyms, string text) { |
| 61 | + vector<int> vinit; |
| 62 | + |
| 63 | + vector<string> textsp = split(text); |
| 64 | + |
| 65 | + unordered_map<int, string> relation; |
| 66 | + unordered_map<string, int> relationrev; |
| 67 | + unordered_map<int, vector<string>> umgroup; |
| 68 | + int count = 0; |
| 69 | + for (auto& s : synonyms) |
| 70 | + { |
| 71 | + if (relationrev.find(s[0]) == relationrev.end()) |
| 72 | + { |
| 73 | + vinit.push_back(count); |
| 74 | + relation[count] = s[0]; |
| 75 | + relationrev[s[0]] = count; |
| 76 | + count++; |
| 77 | + } |
| 78 | + if (relationrev.find(s[1]) == relationrev.end()) |
| 79 | + { |
| 80 | + vinit.push_back(count); |
| 81 | + relation[count] = s[1]; |
| 82 | + relationrev[s[1]] = count; |
| 83 | + count++; |
| 84 | + } |
| 85 | + } |
| 86 | + for (auto& itertextsp : textsp) |
| 87 | + { |
| 88 | + if (relationrev.find(itertextsp) == relationrev.end()) |
| 89 | + { |
| 90 | + vinit.push_back(count); |
| 91 | + relation[count] = itertextsp; |
| 92 | + relationrev[itertextsp] = count; |
| 93 | + count++; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + init(vinit); |
| 99 | + |
| 100 | + for (auto& s : synonyms) |
| 101 | + { |
| 102 | + Merge(relationrev[s[0]], relationrev[s[1]]); |
| 103 | + } |
| 104 | + |
| 105 | + //·Ö×é |
| 106 | + vector<vector<string>> vgroup; |
| 107 | + for (auto& id : vinit) |
| 108 | + { |
| 109 | + umgroup[GetFather(id)].push_back(relation[id]); |
| 110 | + } |
| 111 | + |
| 112 | + for (auto& ium : umgroup) |
| 113 | + vgroup.push_back(ium.second); |
| 114 | + |
| 115 | + //textתÊý×é |
| 116 | + vector<int> resArr; |
| 117 | + |
| 118 | + for (auto& itertextsp : textsp) |
| 119 | + { |
| 120 | + resArr.push_back(GetFather(relationrev[itertextsp])); |
| 121 | + } |
| 122 | + |
| 123 | + vector<vector<string>> resVstr = { textsp }; |
| 124 | + |
| 125 | + for (int i = 0; i < resArr.size(); ++i) |
| 126 | + { |
| 127 | + vector<vector<string>> tempStr; |
| 128 | + for (auto& itersub : umgroup[resArr[i]]) |
| 129 | + { |
| 130 | + for (auto oneKindres : resVstr) |
| 131 | + { |
| 132 | + oneKindres[i] = itersub; |
| 133 | + tempStr.push_back(oneKindres); |
| 134 | + } |
| 135 | + } |
| 136 | + resVstr = tempStr; |
| 137 | + } |
| 138 | + |
| 139 | + vector<string> sRES; |
| 140 | + for (auto& itt : resVstr) |
| 141 | + sRES.push_back(strmerge(itt)); |
| 142 | + |
| 143 | + sort(sRES.begin(), sRES.end()); |
| 144 | + |
| 145 | + return sRES; |
| 146 | + |
| 147 | + } |
| 148 | +}; |
| 149 | + |
| 150 | +/* |
| 151 | +int main() |
| 152 | +{ |
| 153 | + Solution* s = new Solution(); |
| 154 | + vector<vector<string>> synonyms = { {"happy","joy"},{"sad","sorrow"},{"joy","cheerful"} }; |
| 155 | + auto res = s->generateSentences(synonyms, "I am happy today but was sad yesterday"); |
| 156 | + return 0; |
| 157 | +} |
| 158 | +*/ |
0 commit comments