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 <bits/stdc++.h>
using namespace std;
vector<vector<vector<int>>> v;
int ctoi(char c){
if(c == 'A') return 0;
else if(c == 'T') return 1;
return 2;
}
void init(string a, string b){ //O(N)
v = vector<vector<vector<int>>>(a.length());
v[0] = vector<vector<int>>(3, vector<int>(3));
v[0][ctoi(a[0])][ctoi(b[0])] = 1;
for(int i = 1; i < a.length(); i++){
v[i] = v[i-1];
v[i][ctoi(a[i])][ctoi(b[i])]++;
}
}
int get_distance(int x, int y){ //O(1)
//check if possible
vector<vector<int>> dif = v[y];
if(x > 0){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
dif[i][j] -= v[x-1][i][j];
}
}
}
for(int i = 0; i < 3; i++){
int coli, rowi; coli = rowi = 0;
for(int j = 0; j < 3; j++){
coli += dif[i][j];
rowi += dif[j][i];
}
if(coli != rowi) return -1;
}
//calculate total # swaps needed
int mins[3] = {min(dif[0][1], dif[1][0]), min(dif[0][2], dif[2][0]), min(dif[1][2], dif[2][1])};
int maxs[3] = {max(dif[0][1], dif[1][0]), max(dif[0][2], dif[2][0]), max(dif[1][2], dif[2][1])};
int oneswap = mins[0] + mins[1] + mins[2];
int twoswap = (maxs[0]-mins[0]+maxs[1]-mins[1]+maxs[2]-mins[2])*2/3;
return oneswap + twoswap;
}
Compilation message (stderr)
dna.cpp: In function 'void init(std::string, std::string)':
dna.cpp:15:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int i = 1; i < a.length(); i++){
| ~~^~~~~~~~~~~~
# | 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... |