# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1141931 | rayank | Mutating DNA (IOI21_dna) | C++20 | 0 ms | 0 KiB |
#include "dna.h"
#include <bits/stdc++.h>
using namespace std;
string A;
string B;
void init(string a, string b) {
A = a;
B = b;
}
int find_pos(char c, string s, int start, int end){
for(int i = start; i <= end; i++){
if(s[i] == c){
return i;
}
}
return -1;
}
bool issamee(string a, string b, int start, int end){
for(int i = start; i <= end; i++){
if(a[i] != b[i]){
return false;
}
}
return true;
}
int get_distance(int x, int y) {
int res = 0;
string acopy = A;
for(int i=x; i <=y;i++){
if(acopy[i] == B[i]) continue;
int pos = find_pos(acopy[i], B, x, y);
if(pos == -1) return -1;
swap(acopy[i], acopy[pos]);
res++;
}
for(int i=y; i >=x;i--){
if(acopy[i] == B[i]) continue;
int pos = find_pos(acopy[i], B, x, y);
if(pos == -1) return -1;
swap(acopy[i], acopy[pos]);
res++;
}
if(!issamee(acopy, B, x, y)) return -1;
return res;
}
int main() {
int n,q;
cin>>n>>q;
string a,b;
cin>>a>>b;
init(a,b);
while(q--){
int x,y;
cin>>x>>y;
cout<<get_distance(x,y)<<endl;
}
}