#include "dna.h"
#include <bits/stdc++.h>
using namespace std;
const string kDna = "ACT";
const int K = kDna.size();
vector<int> prefix_sum[sizeof(char) << 8][sizeof(char) << 8];
int cnt[sizeof(char) << 8][sizeof(char) << 8];
vector<string> dna_subsets;
constexpr int kImpossible = -1;
void init(string A, string B) {
int N = A.size();
for (int c1 : kDna) {
for (int c2 : kDna) {
prefix_sum[c1][c2].resize(N);
}
}
for (int c1 : kDna) {
for (int c2 : kDna) {
for (int i = 0; i < N; ++i) {
prefix_sum[c1][c2][i] = (c1 == A[i] && c2 == B[i]) ? 1 : 0;
if (i > 0) {
prefix_sum[c1][c2][i] += prefix_sum[c1][c2][i - 1];
}
}
}
}
for (int bit = 1; bit < (1 << K); ++bit) {
string dna_subset;
for (int i = 0; i < K; ++i) {
if (bit & (1 << i)) {
dna_subset += kDna[i];
}
}
dna_subsets.push_back(dna_subset);
}
sort(dna_subsets.begin(), dna_subsets.end(), [] (string a, string b) {
return a.size() < b.size();
});
}
int get_distance(int X, int Y) {
for (int c1 : kDna) {
for (int c2 : kDna) {
cnt[c1][c2] = prefix_sum[c1][c2][Y];
if (X > 0) {
cnt[c1][c2] -= prefix_sum[c1][c2][X - 1];
}
}
}
int answer = Y - X + 1;
for (string dna_subset : dna_subsets) {
do {
int res = INT_MAX;
for (int i = 0; i < static_cast<int>(dna_subset.size()); ++i) {
int c1 = dna_subset[i];
int c2 = dna_subset[(i + 1) % dna_subset.size()];
res = min(res, cnt[c1][c2]);
}
answer -= res;
for (int i = 0; i < static_cast<int>(dna_subset.size()); ++i) {
int c1 = dna_subset[i];
int c2 = dna_subset[(i + 1) % dna_subset.size()];
cnt[c1][c2] -= res;
}
} while (next_permutation(dna_subset.begin(), dna_subset.end()));
}
for (int c1 : kDna) {
for (int c2 : kDna) {
if (cnt[c1][c2] > 0) {
return kImpossible;
}
}
}
return answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
7184 KB |
Output is correct |
2 |
Correct |
42 ms |
7296 KB |
Output is correct |
3 |
Correct |
35 ms |
6952 KB |
Output is correct |
4 |
Correct |
37 ms |
8716 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1912 KB |
Output is correct |
4 |
Correct |
5 ms |
5980 KB |
Output is correct |
5 |
Correct |
5 ms |
5980 KB |
Output is correct |
6 |
Correct |
5 ms |
5980 KB |
Output is correct |
7 |
Correct |
4 ms |
5724 KB |
Output is correct |
8 |
Correct |
5 ms |
5980 KB |
Output is correct |
9 |
Correct |
6 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1912 KB |
Output is correct |
4 |
Correct |
5 ms |
5980 KB |
Output is correct |
5 |
Correct |
5 ms |
5980 KB |
Output is correct |
6 |
Correct |
5 ms |
5980 KB |
Output is correct |
7 |
Correct |
4 ms |
5724 KB |
Output is correct |
8 |
Correct |
5 ms |
5980 KB |
Output is correct |
9 |
Correct |
6 ms |
5980 KB |
Output is correct |
10 |
Correct |
37 ms |
7176 KB |
Output is correct |
11 |
Correct |
38 ms |
7256 KB |
Output is correct |
12 |
Correct |
40 ms |
7452 KB |
Output is correct |
13 |
Correct |
40 ms |
8908 KB |
Output is correct |
14 |
Correct |
43 ms |
8960 KB |
Output is correct |
15 |
Correct |
46 ms |
8808 KB |
Output is correct |
16 |
Correct |
39 ms |
8660 KB |
Output is correct |
17 |
Correct |
40 ms |
8732 KB |
Output is correct |
18 |
Correct |
41 ms |
8980 KB |
Output is correct |
19 |
Correct |
38 ms |
8648 KB |
Output is correct |
20 |
Correct |
37 ms |
8760 KB |
Output is correct |
21 |
Correct |
36 ms |
8980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1912 KB |
Output is correct |
4 |
Correct |
5 ms |
5980 KB |
Output is correct |
5 |
Correct |
5 ms |
5980 KB |
Output is correct |
6 |
Correct |
5 ms |
5980 KB |
Output is correct |
7 |
Correct |
4 ms |
5724 KB |
Output is correct |
8 |
Correct |
5 ms |
5980 KB |
Output is correct |
9 |
Correct |
6 ms |
5980 KB |
Output is correct |
10 |
Correct |
6 ms |
5720 KB |
Output is correct |
11 |
Correct |
6 ms |
6232 KB |
Output is correct |
12 |
Correct |
6 ms |
5980 KB |
Output is correct |
13 |
Correct |
6 ms |
6236 KB |
Output is correct |
14 |
Correct |
6 ms |
6236 KB |
Output is correct |
15 |
Correct |
5 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
7184 KB |
Output is correct |
2 |
Correct |
42 ms |
7296 KB |
Output is correct |
3 |
Correct |
35 ms |
6952 KB |
Output is correct |
4 |
Correct |
37 ms |
8716 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1880 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1912 KB |
Output is correct |
11 |
Correct |
5 ms |
5980 KB |
Output is correct |
12 |
Correct |
5 ms |
5980 KB |
Output is correct |
13 |
Correct |
5 ms |
5980 KB |
Output is correct |
14 |
Correct |
4 ms |
5724 KB |
Output is correct |
15 |
Correct |
5 ms |
5980 KB |
Output is correct |
16 |
Correct |
6 ms |
5980 KB |
Output is correct |
17 |
Correct |
37 ms |
7176 KB |
Output is correct |
18 |
Correct |
38 ms |
7256 KB |
Output is correct |
19 |
Correct |
40 ms |
7452 KB |
Output is correct |
20 |
Correct |
40 ms |
8908 KB |
Output is correct |
21 |
Correct |
43 ms |
8960 KB |
Output is correct |
22 |
Correct |
46 ms |
8808 KB |
Output is correct |
23 |
Correct |
39 ms |
8660 KB |
Output is correct |
24 |
Correct |
40 ms |
8732 KB |
Output is correct |
25 |
Correct |
41 ms |
8980 KB |
Output is correct |
26 |
Correct |
38 ms |
8648 KB |
Output is correct |
27 |
Correct |
37 ms |
8760 KB |
Output is correct |
28 |
Correct |
36 ms |
8980 KB |
Output is correct |
29 |
Correct |
6 ms |
5720 KB |
Output is correct |
30 |
Correct |
6 ms |
6232 KB |
Output is correct |
31 |
Correct |
6 ms |
5980 KB |
Output is correct |
32 |
Correct |
6 ms |
6236 KB |
Output is correct |
33 |
Correct |
6 ms |
6236 KB |
Output is correct |
34 |
Correct |
5 ms |
6236 KB |
Output is correct |
35 |
Correct |
1 ms |
1880 KB |
Output is correct |
36 |
Correct |
36 ms |
8204 KB |
Output is correct |
37 |
Correct |
41 ms |
8716 KB |
Output is correct |
38 |
Correct |
38 ms |
8828 KB |
Output is correct |
39 |
Correct |
44 ms |
9116 KB |
Output is correct |
40 |
Correct |
39 ms |
8976 KB |
Output is correct |
41 |
Correct |
4 ms |
6236 KB |
Output is correct |
42 |
Correct |
39 ms |
8696 KB |
Output is correct |
43 |
Correct |
37 ms |
8984 KB |
Output is correct |
44 |
Correct |
45 ms |
8992 KB |
Output is correct |
45 |
Correct |
35 ms |
8732 KB |
Output is correct |
46 |
Correct |
36 ms |
8976 KB |
Output is correct |
47 |
Correct |
36 ms |
8976 KB |
Output is correct |