-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path612 - DNA Sorting.cpp
87 lines (65 loc) · 1.57 KB
/
612 - DNA Sorting.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <algorithm>
using namespace std;
typedef struct {
char* str;
int length;
int pos;
int value;
} dna_t;
int evaluate(dna_t data);
int compare(const void *a, const void *b);
int main() {
int cases;
cin >> cases;
for (int i = 0; i < cases; i++) {
int n, m;
cin >> n >> m;
dna_t data[m];
for (int j = 0; j < m; j++) {
data[j].str = new char[n + 1];
cin >> data[j].str;
data[j].str[n] = '\0';
data[j].length = n;
data[j].pos = j;
data[j].value = evaluate(data[j]);
}
qsort(data, m, sizeof(dna_t), compare);
for (int j = 0; j < m; j++) {
cout << data[j].str << endl;
delete data[j].str;
}
if (i < cases - 1)
cout << endl;
}
return 0;
}
int evaluate(dna_t data) {
int alphabet[4] = {0};
int result = 0;
for (int i = data.length - 1; i >= 0; i--) {
char c = data.str[i];
int index;
if (c == 'A') {
index = 0;
} else if (c == 'C') {
index = 1;
} else if (c == 'G') {
index = 2;
} else {
index = 3;
}
alphabet[index]++;
for (int j = index - 1; j >= 0; j--) {
result += alphabet[j];
}
}
return result;
}
int compare(const void *a, const void *b) {
auto* x = (dna_t*) a;
auto* y = (dna_t*) b;
if (x->value != y->value)
return x->value - y->value;
return x->pos - y->pos;
}