File tree Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change 1+ //
2+ // Created by 10578 on 2021/3/30.
3+ //
4+
5+ #include " vector"
6+ #include " string"
7+ #include " unordered_map"
8+ #include " queue"
9+
10+ using namespace std ;
11+
12+ class Solution {
13+ public:
14+ string evaluate (const string& s, vector<vector<string>>& knowledge) {
15+ unordered_map<string, string> k;
16+ for (auto &&item : knowledge) {
17+ k[item[0 ]] = item[1 ];
18+ }
19+
20+ deque<char > dq;
21+ for (auto &&c: s) {
22+ dq.push_back (c);
23+ if (dq.back () == ' )' ) {
24+ // pop the ")"
25+ dq.pop_back ();
26+
27+ string key;
28+ key.reserve (10 );
29+
30+ char c;
31+ do {
32+ c = dq.back ();
33+ dq.pop_back ();
34+ key += c;
35+ } while (dq.back () != ' (' );
36+
37+ // pop the "("
38+ dq.pop_back ();
39+
40+ reverse (key.begin (), key.end ());
41+
42+ string value = k.count (key) == 0 ? " ?" : k[key];
43+ for (auto &&ch: value) {
44+ dq.push_back (ch);
45+ }
46+ }
47+ }
48+
49+ string result (dq.begin (), dq.end ());
50+
51+ return result;
52+ }
53+ };
54+
55+ int main (){
56+
57+ return 0 ;
58+ }
Original file line number Diff line number Diff line change 1+ add_executable (medium 1807_EvaluateTheBracketPairsOfAString.cpp)
You can’t perform that action at this time.
0 commit comments