#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag,
// __gnu_pbds::tree_order_statistics_node_update > ordered_set;
// #include "debugging.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define f first
#define s second
#define endl '\n'
#define fr(i, x) for (int i = 0; i < x; i++)
#define fr1(i, x) for (int i = 1; i <= x; i++)
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define lcm(a, b) (a * b) / __gcd(a, b)
#define sqr(x) ((x) * (x))
#define cube(x) ((x) * (x) * (x))
#define dbg(v) cerr << "Line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl;
#define show(v) cerr << v << " ";
#define INT(n) \
int n; \
cin >> n;
const int INF = 1e6;
const double PI = acos(-1);
const int MOD = 1e9 + 7;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
string scary, fav;
cin >> n >> m;
vvi dist(n + 1, vi(m + 1, INF));
cin >> scary >> fav;
fr(i, n) if (scary[i] == fav[0]) dist[i][0] = 0;
fr1(i, m - 1) {
fr(j, n) {
if (scary[j] == fav[i]) {
for (auto k : {j + 1, j - 1}) {
if (k >= 0 && k < n)
dist[j][i] = min(dist[j][i], dist[k][i - 1] + abs(j - k));
}
}
}
fr(j, n) {
fr(k, n) {
if (scary[j] == scary[k]) {
dist[j][i] = min(dist[j][i], dist[k][i] + abs(j - k));
}
}
}
}
int best = INF;
fr(i, n) best = min(best, dist[i][m - 1]);
if (best == INF)
cout << "-1\n";
else
cout << best << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
812 KB |
Output is correct |
2 |
Correct |
54 ms |
604 KB |
Output is correct |
3 |
Correct |
30 ms |
596 KB |
Output is correct |
4 |
Correct |
37 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
33 ms |
604 KB |
Output is correct |
8 |
Correct |
44 ms |
604 KB |
Output is correct |
9 |
Correct |
81 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
34 ms |
604 KB |
Output is correct |