# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1052847 | amongus_pvp | Mutating DNA (IOI21_dna) | C++17 | 22 ms | 6360 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int ALPHABET_SIZE = 3;
vector<vector<int>> prefixSumA;
vector<vector<int>> prefixSumB;
void init(string a, string b) {
int n = a.size();
// Initialize prefix sum arrays
prefixSumA.assign(ALPHABET_SIZE, vector<int>(n + 1, 0));
prefixSumB.assign(ALPHABET_SIZE, vector<int>(n + 1, 0));
// Mapping from character to index: A -> 0, T -> 1, C -> 2
auto charToIndex = [](char c) -> int {
if (c == 'A') return 0;
if (c == 'T') return 1;
return 2; // 'C'
};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < ALPHABET_SIZE; ++j) {
prefixSumA[j][i + 1] = prefixSumA[j][i];
prefixSumB[j][i + 1] = prefixSumB[j][i];
}
prefixSumA[charToIndex(a[i])][i + 1]++;
prefixSumB[charToIndex(b[i])][i + 1]++;
}
}
int get_distance(int x, int y) {
vector<int> freqA(ALPHABET_SIZE), freqB(ALPHABET_SIZE);
for (int i = 0; i < ALPHABET_SIZE; ++i) {
freqA[i] = prefixSumA[i][y + 1] - prefixSumA[i][x];
freqB[i] = prefixSumB[i][y + 1] - prefixSumB[i][x];
}
if (freqA != freqB) return -1;
// Calculate the number of mutations required
string subA = "", subB = "";
for (int i = x; i <= y; ++i) {
subA += freqA[0]-- > 0 ? 'A' : (freqA[1]-- > 0 ? 'T' : 'C');
subB += freqB[0]-- > 0 ? 'A' : (freqB[1]-- > 0 ? 'T' : 'C');
}
int swaps = 0;
for (int i = 0; i < subA.size(); ++i) {
if (subA[i] != subB[i]) {
swaps++;
}
}
return swaps / 2;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |