This repository was archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathreverse_shuffle.cpp
More file actions
67 lines (50 loc) · 1.41 KB
/
reverse_shuffle.cpp
File metadata and controls
67 lines (50 loc) · 1.41 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
// https://www.hackerrank.com/challenges/reverse-shuffle-merge
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
char S[10240];
int cnt[10240][26], sub[26];
int main() {
scanf("%s", S);
int N = strlen(S);
memset(cnt,0,sizeof(cnt));
reverse(S, S+N);
for (int p = N-1; p >= 0; p--) {
copy(cnt[p+1], cnt[p+1]+26, cnt[p]);
cnt[p][S[p]-'a'] += 1;
}
copy(cnt[0], cnt[0]+26, sub);
for (int c = 0; c < 26;c++)
sub[c] /= 2;
int startPoint = -1, endPoint = 0;
for (int i=0; i+i<N; i++) {
do {
startPoint += 1;
} while (sub[S[startPoint] - 'a'] == 0);
for (int p = startPoint; p < N; p++) {
if ((sub[S[p]-'a']>0) && (S[p] < S[startPoint])) {
if (p < endPoint)
startPoint = p;
else {
int cmpres = 1;
for (int c = 0; c < 26; c++)
if (cnt[p][c] < sub[c]) {
cmpres = -1;
break;
}
if (cmpres == -1) break;
startPoint = p;
endPoint += 1;
}
}
}
sub[S[startPoint] - 'a'] -= 1;
putchar(S[startPoint]);
}
puts("");
return 0;
}