#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){
//cout<<i<<" "<<j<<"\n";
if(i <= -1 or i >= a.size() or a[i] != b[j]) return -1;
long long Retorno = -1;
if(Memorizaci_n[i][j] != -2) return Memorizaci_n[i][j];
else if(j >= b.size() - 1) return 0;
for(long long k = 0; k < a.size(); k++){
if(a[k] == a[i]){
if(k - 1 > -1 and a[k - 1] == b[j + 1]){
long long A = Resolver(k - 1, j + 1);
if(A != -1){
if(Retorno == -1) Retorno = A + abs(i - k) + 1;
else Retorno = min(Retorno, A + abs(i - k) + 1);
}
}
if(k + 1 < a.size() and a[k + 1] == b[j + 1]){
long long A = Resolver(k + 1, j + 1);
if(A != -1){
if(Retorno == -1) Retorno = A + abs(i - k) + 1;
else Retorno = min(Retorno, A + abs(i - k) + 1);
}
}
}
}
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 = -1;
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;
}
}
/*for(auto E: Memorizaci_n){
for(auto e: E) cout<<e<<" ";
cout<<"\n";
}*/
cout<<Respuesta;
return 0;
}
Compilation message
bajka.cpp: In function 'long long int Resolver(long long int, long long int)':
bajka.cpp:7:21: 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]
7 | if(i <= -1 or i >= a.size() or a[i] != b[j]) return -1;
| ~~^~~~~~~~~~~
bajka.cpp:10: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]
10 | else if(j >= b.size() - 1) return 0;
| ~~^~~~~~~~~~~~~~~
bajka.cpp:11:28: 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]
11 | for(long long k = 0; k < a.size(); k++){
| ~~^~~~~~~~~~
bajka.cpp:20:22: 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]
20 | if(k + 1 < a.size() and a[k + 1] == b[j + 1]){
| ~~~~~~^~~~~~~~~~
bajka.cpp: In function 'int main()':
bajka.cpp:41: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]
41 | for(long long i = 0; i < Posibles.size(); i++){
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
1112 KB |
Output is correct |
8 |
Correct |
32 ms |
1116 KB |
Output is correct |
9 |
Correct |
48 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
3 ms |
1112 KB |
Output is correct |