Submission #482469

# Submission time Handle Problem Language Result Execution time Memory
482469 2021-10-24T15:56:14 Z jalsol Bajka (COCI20_bajka) C++17
70 / 70
151 ms 704 KB
#ifdef LOCAL
#include "local.h"
#else
#include <bits/stdc++.h>
#define debug(...)
#define DB(...)
#endif

using namespace std;

using ll = long long;
using ld = long double;

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

#define For(i, l, r) for (int i = (l); i <= (r); ++i)
#define Ford(i, r, l) for (int i = (r); i >= (l); --i)
#define Rep(i, n) For (i, 0, (n) - 1)
#define Repd(i, n) Ford (i, (n) - 1, 0)
#define Fe(...) for (auto __VA_ARGS__)

template <class C> inline int isz(const C& c) { return static_cast<int>(c.size()); }
template <class T> inline bool chmin(T& a, const T& b) { return (a > b) ? a = b, true : false; }
template <class T> inline bool chmax(T& a, const T& b) { return (a < b) ? a = b, true : false; }

const bool __initialization = []() {
    cin.tie(nullptr)->sync_with_stdio(false);
#define TASK "empty"
    if (fopen(TASK".inp", "r")) {
        (void)(!freopen(TASK".inp", "r", stdin));
        (void)(!freopen(TASK".out", "w", stdout)); }
    cout << setprecision(12) << fixed;
    return true;
}();

constexpr ld eps = 1e-9;
constexpr int inf = 1e9;
constexpr ll linf = 1e18;

// =============================================================================

constexpr int maxn = 300 + 5;

struct State {
    int i, pos;
};

int n, m;
char s[maxn], t[maxn];

int d[maxn][maxn];
queue<State> q;

void Bfs() {
    while (isz(q)) {
        auto [i, pos] = q.front(); q.pop(); 
        if (pos == m) continue;

        Fe (j : { i - 1, i + 1 }) {
            if (j == 0 || j == n + 1) continue;

            if (s[j] == t[pos + 1]) {
                if (chmin(d[j][pos + 1], d[i][pos] + 1)) {
                    q.push({ j, pos + 1 });
                }
            }
        }

        For (j, 1, n) {
            if (s[i] != s[j] || i == j) continue;
            if (chmin(d[j][pos], d[i][pos] + abs(i - j))) {
                q.push({ j, pos });
            }
        }
    }
}

signed main() {
    cin >> n >> m;
    cin >> (s + 1) >> (t + 1);

    memset(d, 0x3f, sizeof(d));

    For (i, 1, n) {
        if (s[i] == t[1]) {
            d[i][1] = 0;
            q.push({ i, 1 });
        }
    }

    Bfs();

    int ans = inf;
    For (i, 1, n) chmin(ans, d[i][m]);

    if (ans == inf) ans = -1;
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 588 KB Output is correct
2 Correct 12 ms 684 KB Output is correct
3 Correct 8 ms 588 KB Output is correct
4 Correct 12 ms 588 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 55 ms 680 KB Output is correct
8 Correct 151 ms 704 KB Output is correct
9 Correct 46 ms 588 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
11 Correct 57 ms 668 KB Output is correct