# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1141993 | rayank | 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;
}
int get_distance(int x, int y) {
int res = 0;
string suba = A.substr(x, y);
string subb = B.substr(x,y);
string subacopy = suba;
string subbcopy = subb;
sort(subacopy.begin(), subacopy.end());
sort(subbcopy.begin(), subbcopy.end());
if(subbcopy != subacopy){
return -1;
}
while(suba != subb){
for(int i=0; i <suba.size();i++){
if(suba[i] == subb[i]) continue;
int pos = find_pos(suba[i], subb, 0, y-x+1);
swap(suba[i], suba[pos]);
res++;
}
}
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;
}
}