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>
// #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]) {
fr(k, n) {
if (j != k)
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |