-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3170.cpp
55 lines (48 loc) · 1.07 KB
/
3170.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
55
#include "leetcode.hpp"
bool compare(pair<char, int> a, pair<char, int> b) {
if (a.first > b.first) {
return true;
} else if (a.first == b.first) {
return a.second < b.second;
} else {
return false;
}
}
class Solution {
public:
string clearStars(string s) {
string ret = "";
priority_queue<pair<char, int>, vector<pair<char, int>>,
function<bool(pair<char, int>, pair<char, int>)>>
q(compare);
for (auto i = 0; i < s.size(); ++i) {
if (s[i] == '*') {
if (!q.empty()) {
q.pop();
}
} else {
q.push(make_pair(s[i], i));
}
}
vector<pair<int, char>> res;
while (!q.empty()) {
res.push_back(make_pair(q.top().second, q.top().first));
q.pop();
}
sort(res.begin(), res.end());
for (auto p : res) {
ret += p.second;
}
return ret;
}
};
int main(int argc, char const *argv[]) {
Solution s;
string t1 = "dk**";
string t2 = "aaba*";
string t3 = "de*";
s.clearStars(t3);
s.clearStars(t2);
s.clearStars(t1);
return 0;
}