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;
#define int long long
#define pii pair<long long, long long>
string a, b;
vector<vector<vector<int>>> pref;
vector<vector<int>> apref, bpref;
void init(string a_, string b_) {
vector<int> mp(256, -1);
mp['A'] = 0, mp['C'] = 1, mp['T'] = 2;
a = a_, b = b_;
int n = a.size();
pref.resize(3, vector<vector<int>>(3, vector<int>(n + 1)));
apref.resize(3, vector<int>(n + 1));
bpref.resize(3, vector<int>(n + 1));
for (int i = 0; i < n; i++) {
for (int c1 = 0; c1 < 3; c1++) {
for (int c2 = 0; c2 < 3; c2++) {
pref[c1][c2][i + 1] = pref[c1][c2][i] + (c1 == mp[a[i]] && c2 == mp[b[i]]);
}
}
for (int c = 0; c < 3; c++) {
apref[c][i + 1] = apref[c][i] + (c == mp[a[i]]);
bpref[c][i + 1] = bpref[c][i] + (c == mp[b[i]]);
}
}
/*for (auto [str, vec] : apref) {
cout << str << ": ";
for (int x : vec) cout << x << " ";
cout << "\n";
}*/
}
int get_distance(signed l, signed r) {
vector<vector<int>> cnt(3, vector<int>(3));
vector<int> acnt(3), bcnt(3);
for (int c1 = 0; c1 < 3; c1++) {
for (int c2 = 0; c2 < 3; c2++) {
cnt[c1][c2] = pref[c1][c2][r + 1] - pref[c1][c2][l];
}
acnt[c1] = apref[c1][r + 1] - apref[c1][l];
bcnt[c1] = bpref[c1][r + 1] - bpref[c1][l];
}
for (int chr = 0; chr < 3; chr++) {
if (acnt[chr] != bcnt[chr]) return -1;
}
int atflip = min(cnt[2][0], cnt[0][2]);
int acflip = min(cnt[1][0], cnt[0][1]);
int ctflip = min(cnt[2][1], cnt[1][2]);
int ans = atflip + acflip + ctflip;
cnt[2][0] -= atflip;
cnt[0][2] -= atflip;
cnt[2][1] -= ctflip;
cnt[1][2] -= ctflip;
cnt[1][0] -= acflip;
cnt[0][1] -= acflip;
ans += (cnt[2][0] + cnt[0][2] + cnt[2][1] + cnt[1][2] + cnt[1][0] + cnt[0][1]) / 3 * 2;
return ans;
}
#ifdef ONLINE_JUDGE
signed main() {
init("ATACAT", "ACTATA");
cout << get_distance(1, 3) << "\n";
cout << get_distance(4, 5) << "\n";
cout << get_distance(3, 5) << "\n";
}
#endif
# | 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... |