답안 #366384

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
366384 2021-02-14T06:25:07 Z kartel Bajka (COCI20_bajka) C++14
50 / 70
71 ms 780 KB
#include <bits/stdc++.h>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
#define M ll(1e9 + 7)
#define sz(x) (int)x.size()
#define re return
#define oo ll(1e18)
#define el '\n'
#define pii pair <int, int>
#define all(x) (x).begin(), (x).end()
#define arr_all(x, n) (x + 1), (x + 1 + n)
#define vi vector<int>
using namespace std;
typedef long long ll;
//using namespace __gnu_pbds;
//typedef tree <ll, null_type, less_equal <ll> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
//typedef long double ld;
typedef unsigned long long ull;

const int N = 310;

int f[N][N], n, m;
string s, t;
vector <int> in[300];

int main()
{
//    mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());;
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//  in("pixels.in");
//  out("pixels.out");
//    in("input.txt");
//    out("output.txt");

    cin >> n >> m >> s >> t;

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            f[i][j] = 1e9;
    }

    for (int i = 0; i < sz(s); i++) {
        in[s[i]].pb(i);

        if (s[i] == t[0])
            f[0][i] = 0;
    }

    for (int i = 1; i < m; i++) {
        for (int j = 0; j < sz(in[t[i - 1]]); j++) {
            int p = in[t[i - 1]][j];

            for (auto y : in[t[i - 1]]) {
                int cnt = f[i - 1][p] + abs(p - y) + 1;

                int np = max(0, y - 1);

                if (s[np] == t[i]) {
                    f[i][np] = min(f[i][np], cnt);
                }

                np = min(n - 1, y + 1);

                if (s[np] == t[i]) {
                    f[i][np] = min(f[i][np], cnt);
                }
            }
        }
    }
    int mn = *min_element(f[m - 1], f[m - 1] + n);

    cout << ((mn == 1e9) ? -1 : mn);
}

Compilation message

bajka.cpp: In function 'int main()':
bajka.cpp:51:16: warning: array subscript has type 'char' [-Wchar-subscripts]
   51 |         in[s[i]].pb(i);
      |                ^
bajka.cpp:58:43: warning: array subscript has type 'char' [-Wchar-subscripts]
   58 |         for (int j = 0; j < sz(in[t[i - 1]]); j++) {
      |                                           ^
bajka.cpp:13:20: note: in definition of macro 'sz'
   13 | #define sz(x) (int)x.size()
      |                    ^
bajka.cpp:59:32: warning: array subscript has type 'char' [-Wchar-subscripts]
   59 |             int p = in[t[i - 1]][j];
      |                                ^
bajka.cpp:61:38: warning: array subscript has type 'char' [-Wchar-subscripts]
   61 |             for (auto y : in[t[i - 1]]) {
      |                                      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 748 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 748 KB Output is correct
2 Correct 4 ms 780 KB Output is correct
3 Correct 1 ms 748 KB Output is correct
4 Correct 2 ms 748 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 8 ms 748 KB Output is correct
8 Correct 37 ms 748 KB Output is correct
9 Correct 71 ms 748 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 5 ms 768 KB Output is correct