forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
32 lines (32 loc) · 869 Bytes
/
Solution.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
class Solution {
public:
string entityParser(string text) {
unordered_map<string, string> d = {
{""", "\""},
{"'", "'"},
{"&", "&"},
{">", ">"},
{"<", "<"},
{"⁄", "/"},
};
string ans = "";
int i = 0, n = text.size();
while (i < n) {
bool found = false;
for (int l = 1; l < 8; ++l) {
int j = i + l;
if (j <= n) {
string t = text.substr(i, l);
if (d.count(t)) {
ans += d[t];
i = j;
found = true;
break;
}
}
}
if (!found) ans += text[i++];
}
return ans;
}
};