#include <bits/stdc++.h>
using namespace std;
/*
Rucode: jaqVYNrpMj
JUDGE_ID: 295965SY
dl:160532
*/
void data() {
#ifdef NURS
freopen("main.in", "r", stdin);
freopen("main.out", "w", stdout);
#endif
}
void win()
{
ios_base::sync_with_stdio(0),
cin.tie(0),cout.tie(0);
}
int n, m, add, ans;
string a, b;
int was[350][350], dp[350][350];
vector<int> g[30];
int rec(int pos, int le)
{
// cout << pos << " " << le << '\n';
if (le == b.size())
return 0;
if (was[pos][le])
return dp[pos][le];
add = 1e9;
for (int i = 0; i < g[a[pos] - 'a'].size(); i++)
{
int to = g[a[pos] - 'a'][i];
if (to - 1 >= 0 && a[to - 1] == b[le])
add = min(add, rec(to - 1, le + 1) + abs(to - pos) + 1);
if (to + 1 < a.size() && a[to + 1] == b[le])
add = min(add, rec(to + 1, le + 1)+ abs(to - pos) + 1);
}
was[pos][le] = 1;
dp[pos][le] = add;
return add;
}
int main()
{
data();
win();
cin >> n >> m;
cin >> a >> b;
for (int i = 0; i < a.size(); i++)
g[a[i] - 'a'].push_back(i);
ans = 1e9;
for (int i = 0; i < a.size(); i++)
if (a[i] == b[0])
ans = min(ans, rec(i, 1));
ans += (ans == 1e9) * (-ans - 1);
cout << ans;
}
Compilation message
bajka.cpp: In function 'int rec(int, int)':
bajka.cpp:27:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | if (le == b.size())
| ~~~^~~~~~~~~~~
bajka.cpp:33:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for (int i = 0; i < g[a[pos] - 'a'].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~~~~~
bajka.cpp:38:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if (to + 1 < a.size() && a[to + 1] == b[le])
| ~~~~~~~^~~~~~~~~~
bajka.cpp: In function 'int main()':
bajka.cpp:51:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 0; i < a.size(); i++)
| ~~^~~~~~~~~~
bajka.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0; i < a.size(); i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1132 KB |
Output is correct |
2 |
Incorrect |
2 ms |
1220 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |