Submission #699494

# Submission time Handle Problem Language Result Execution time Memory
699494 2023-02-17T06:11:50 Z Chal1shkan Bajka (COCI20_bajka) C++14
70 / 70
86 ms 1024 KB
# include <bits/stdc++.h>

# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x  << " = " << x << endl; 

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ll maxn = 1e5 + 25;
const ll inf = 1e18 + 0;
const ll mod = 1e9 + 7;
const ll dx[] = {0, -1, 1, 0};
const ll dy[] = {1, 0, 0, -1};
const ld eps = 1e-6;

using namespace std;

ll n, m;
string s, t;
ll dp[303][303];

void ma1n ()
{
    cin >> n >> m;
    cin >> s >> t;
    s = "$" + s;
    t = "$" + t;
    for (ll i = 0; i <= n; ++i)
    {
        for (ll j = 0; j <= m; ++j)
        {
            dp[i][j] = inf;
        }
    }
    for (ll i = 1; i <= n; ++i)
    {
        if (s[i] == t[1])
        {
            dp[i][1] = 0;
        }
    }
    for (ll j = 1; j <= m; ++j)
    {
        for (ll i = 1; i <= n; ++i)
        {
            if (s[i] == t[j])
            {
                if (i > 1)
                {
                    dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1);
                }
                if (i < n)
                {
                    dp[i][j] = min(dp[i][j], dp[i + 1][j - 1] + 1);
                }
            }
        }
        for (ll i = 1; i <= n; ++i)
        {
            for (ll k = 1; k <= n; ++k)
            {
                if (s[i] == s[k])
                {
                    dp[i][j] = min(dp[i][j], dp[k][j] + abs(i - k));
                }
            }
        }
    }
    ll ans = inf;
    for (ll i = 1; i <= n; ++i)
    {
        ans = min(ans, dp[i][m]);
    }
    if (ans == inf) ans = -1;
    cout << ans << nl;

}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//  freopen("file.in", "r", stdin);
//  freopen("file.out", "w", stdout);
    int ttt = 1;
//  cin >> ttt;
    for (int test = 1; test <= ttt; ++test)
    {
//      cout << "Case " << test << ":" << ' ';
        ma1n();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 268 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 1024 KB Output is correct
2 Correct 46 ms 1024 KB Output is correct
3 Correct 45 ms 980 KB Output is correct
4 Correct 35 ms 980 KB Output is correct
5 Correct 3 ms 972 KB Output is correct
6 Correct 3 ms 968 KB Output is correct
7 Correct 53 ms 1020 KB Output is correct
8 Correct 52 ms 1024 KB Output is correct
9 Correct 86 ms 980 KB Output is correct
10 Correct 2 ms 980 KB Output is correct
11 Correct 70 ms 972 KB Output is correct