# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1067370 | AlgorithmWarrior | Mutating DNA (IOI21_dna) | C++17 | 0 ms | 0 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 "dna.h"
#include <bits/stdc++.h>
#define MAX 100005
using namespace std;
int sp[MAX][3][3];
int sp1[MAX][3];
int sp2[MAX][3];
void init(string a, string b)
{
int to[100];
to['A']=0;
to['C']=1;
to['T']=2;
int n=a.size();
int i,j,k;
for(i=1;i<=n;++i)
{
++sp[i][to[a[i-1]]][to[b[i-1]]];
++sp1[i][to[a[i-1]]];
++sp2[i][to[b[i-1]]];
for(j=0;j<3;++j)
sp1[i][j]+=sp1[i-1][j];
for(j=0;j<3;++j)
sp2[i][j]+=sp2[i-1][j];
for(j=0;j<3;++j)
for(k=0;k<3;++k)
sp[i][j][k]+=sp[i-1][j][k];
}
}
int get_distance(int x, int y)
{
++x,++y;
int i,j;
for(i=0;i<3;++i)
if(sp1[y][i]-sp1[x-1][i]!=sp2[y][i]-sp2[x-1][i])
return -1;
int nr=abs(sp[y][0][1]-sp[x-1][0][1]-sp[y][1][0]+sp[x-1][1][0]);
int cnt=2*nr;
for(i=0;i<3;++i)
for(j=i+1;j<3;++j)
cnt+=min(sp[y][i][j]-sp[x-1][i][j],sp[y][j][i]-sp[x-1][j][i]);
return cnt;
}
int main() {
int n, q;
assert(scanf("%d %d", &n, &q) == 2);
char A[n+1], B[n+1];
assert(scanf("%s", A) == 1);
assert(scanf("%s", B) == 1);
std::string a = std::string(A);
std::string b = std::string(B);
std::vector<int> x(q), y(q);
for (int i = 0; i < q; i++) {
assert(scanf("%d %d", &x[i], &y[i]) == 2);
}
fclose(stdin);
std::vector<int> results(q);
init(a, b);
for (int i = 0; i < q; i++) {
results[i] = get_distance(x[i], y[i]);
}
for (int i = 0; i < q; i++) {
printf("%d\n", results[i]);
}
fclose(stdout);
return 0;
}