-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreshuffle_char.cpp
55 lines (53 loc) · 1.33 KB
/
reshuffle_char.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
nclude <iostream>
#include <vector>
#include <map>
#include <queue>
#include <climits>
using namespace std;
string rearrangechar(string str, int k){
if (str.empty()||str.size() < k)
return str;
int len = str.size();
string res="";
typedef pair<char,pair<int,int>> Node;
map<char,Node> m;
for (int i=0; i<len;i++){
m[str[i]].first = str[i];
m[str[i]].second.first++;
}
queue<Node> q;
struct mycompare{
bool operator()(const Node &a, const Node &b){
if (a.second.first != b.second.first)
return a.second.first > b.second.first? 0:1;
return a.first - b.first? 1:0;
}
};
priority_queue<Node,vector<Node>,mycompare> pq;
for (auto &node: m)
pq.push(node.second);
for (int i = 0;i<len;i++){
if (!q.empty() && (q.front().second.second + k < i)){
pq.push(q.front());
q.pop();
}
if (pq.empty()){
return "invalid";
}
Node cur = pq.top();
pq.pop();
cur.second.second = i;
cur.second.first --;
res += cur.first;
if (cur.second.first)
q.push(cur);
}
return res;
}
int main() {
// your code goes here
string s{"aaaaabbbcccddee"};
string res = rearrangechar(s, 2);
cout<<res<<endl;
return 0;
}