-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
46 lines (33 loc) · 779 Bytes
/
common.cpp
File metadata and controls
46 lines (33 loc) · 779 Bytes
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
#include "common.h"
void takeLastColumn(char* str, int* suffArray, int n, char* result) {
for(int i = 0; i < n; i++) {
result[i] = str[(suffArray[i] + n - 1) % n];
}
}
char* bwDecode(char* encoded, int n) {
const int MAXCHAR = 256;
int cnt[MAXCHAR];
int *id = new int[n];
for(int i = 0; i < MAXCHAR; i++) {
cnt[i] = 0;
}
for(int i = 0; i < n; i++) {
char c = encoded[i];
id[i] = cnt[c]++;
}
for(int i = 1; i < MAXCHAR; i++) {
cnt[i] += cnt[i-1];
}
char *decoded = new char[n];
int j = n - 1;
decoded[n - 1] = END_SYMBOL;
for(int i = n - 2; i >= 0; i--) {
char c = encoded[j];
decoded[i] = c;
int prevEqual = id[j];
int totalSmaller = (c > 0 ? cnt[c-1] : 0);
j = totalSmaller + prevEqual;
}
delete[] id;
return decoded;
}