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;
string a, b;
vector< vector<long long> > Memorizaci_n;
long long Resolver(long long i, long long j){
long long Retorno = 2222222222222222;
if(Memorizaci_n[i][j] != -2) return Memorizaci_n[i][j];
else if(j >= b.size() - 1) return 0;
Memorizaci_n[i][j] = -1;
if(i == 0 and a[i + 1] == b[j + 1]){
long long A = Resolver(i + 1, j + 1);
if(A == -1) A = 2222222222222221;
Retorno = min(Retorno, A + 1);
} else if(i == a.size() - 1 and a[i - 1] == b[j + 1]){
long long A = Resolver(i - 1, j + 1);
if(A == -1) A = 2222222222222221;
Retorno = min(Retorno, A + 1);
} else {
if(a[i + 1] == b[j + 1]){
long long A = Resolver(i + 1, j + 1);
if(A == -1) A = 2222222222222221;
Retorno = min(Retorno, A + 1);
}
if(a[i - 1] == b[j + 1]){
long long A = Resolver(i - 1, j + 1);
if(A == -1) A = 2222222222222221;
Retorno = min(Retorno, A + 1);
}
}
for(long long k = i + 1; k < a.size(); k++){
if(a[k] == a[i]){
long long A = Resolver(k, j);
if(A == -1) A = 2222222222222222 - abs(i - k);
Retorno = min(Retorno, A + abs(i - k));
break;
}
}
for(long long k = i - 1; k > -1; k--){
if(a[k] == a[i]){
long long A = Resolver(k, j);
if(A == -1) A = 2222222222222222 - abs(i - k);
Retorno = min(Retorno, A + abs(i - k));
break;
}
}
if(Retorno == 2222222222222222) return Memorizaci_n[i][j] = -1;
else return Memorizaci_n[i][j] = Retorno;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m;
cin>>n>>m>>a>>b;
Memorizaci_n.assign(n, vector<long long>(m, -2));
long long Respuesta = 2222222222222222;
vector<long long> Posibles;
for(long long i = 0; i < n; i++) if(a[i] == b[0]) Posibles.push_back(Resolver(i, 0));
sort(Posibles.begin(), Posibles.end());
for(long long i = 0; i < Posibles.size(); i++){
if(Posibles[i] != -1){
Respuesta = Posibles[i];
break;
}
}
if(Respuesta == 2222222222222222) Respuesta = -1;
cout<<Respuesta;
return 0;
}
Compilation message (stderr)
bajka.cpp: In function 'long long int Resolver(long long int, long long int)':
bajka.cpp:8:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | else if(j >= b.size() - 1) return 0;
| ~~^~~~~~~~~~~~~~~
bajka.cpp:14:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | } else if(i == a.size() - 1 and a[i - 1] == b[j + 1]){
| ~~^~~~~~~~~~~~~~~
bajka.cpp:30:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(long long k = i + 1; k < a.size(); k++){
| ~~^~~~~~~~~~
bajka.cpp: In function 'int main()':
bajka.cpp:59:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(long long i = 0; i < Posibles.size(); i++){
| ~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |