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 <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int word(string s, string t, int n, int m, int j, int k, bool cambio){
int tiempo=1000000000;
if (j==m) return 0;
if (k-1>=0 and s[k-1]==t[j]){
int poss=word(s, t, n, m, j+1, k-1, false);
tiempo=min(tiempo, poss+1);
}if (k+1<n and s[k+1]==t[j]){
int poss=word(s, t, n, m, j+1, k+1, false);
tiempo=min(tiempo, poss+1);
}for (int l=0; l<n; l++){
if (s[l]==s[k] and l!=k and cambio==false){
int poss=word(s, t, n, m, j, l, true);
tiempo=min(tiempo, poss+abs(k-l));
}
}return tiempo;
}
int main(){
int n, m;
cin>>n>>m;
string s, t;
cin>>s>>t;
int ans=1e9;
for (int i=0; i<n; i++){
if (s[i]==t[0]) ans=min(ans, word(s, t, n, m, 1, i, true));
}if (ans==1e9) cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |