답안 #321742

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
321742 2020-11-13T08:45:45 Z alextodoran Bajka (COCI20_bajka) C++17
20 / 70
1000 ms 33980 KB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 302;
const int M_MAX = 302;

int n, m;

string a, b;

ll dp[M_MAX][N_MAX];

struct State
{
    int pref;
    int pos;
};

struct Path
{
    State s;
    ll len;
};

bool operator < (const Path &a, const Path &b)
{
    return a.len > b.len;
}

priority_queue <Path> pq;

bool visited[M_MAX][N_MAX];

vector <int> vch[26];

void Dijkstra ()
{
    for(int i = 1; i <= n; i++)
        if(a[i] == b[1])
            pq.push(Path{State{1, i}, 0});
    while(pq.empty() == false)
    {
        Path p = pq.top();
        pq.pop();
        if(visited[p.s.pref][p.s.pos] == true)
            continue;
        visited[p.s.pref][p.s.pos] = true;
        dp[p.s.pref][p.s.pos] = p.len;
        if(p.s.pref == m)
            continue;
        if(p.s.pos > 1 && a[p.s.pos - 1] == b[p.s.pref + 1] && visited[p.s.pref + 1][p.s.pos - 1] == false)
            pq.push(Path{State{p.s.pref + 1, p.s.pos - 1}, p.len + 1});
        if(p.s.pos < n && a[p.s.pos + 1] == b[p.s.pref + 1] && visited[p.s.pref + 1][p.s.pos + 1] == false)
            pq.push(Path{State{p.s.pref + 1, p.s.pos + 1}, p.len + 1});
        for(int pos1 : vch[a[p.s.pos] - 'a'])
            if(pos1 != p.s.pos)
            {
                if(visited[p.s.pref][pos1] == false)
                    pq.push(Path{State{p.s.pref, pos1}, p.len + abs(p.s.pos - pos1)});
            }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    cin >> a >> b;
    a = " " + a;
    b = " " + b;
    for(int i = 1; i <= n; i++)
        vch[a[i] - 'a'].push_back(i);
    Dijkstra();
    ll ans = LLONG_MAX;
    for(int i = 1; i <= n; i++)
        if(visited[m][i] == true)
            ans = min(ans, dp[m][i]);
    if(ans == LLONG_MAX)
        ans = -1;
    cout << ans << "\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 1132 KB Output is correct
5 Correct 1 ms 1132 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 876 KB Output is correct
2 Correct 41 ms 1644 KB Output is correct
3 Correct 8 ms 1260 KB Output is correct
4 Correct 12 ms 1388 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 124 ms 3172 KB Output is correct
8 Execution timed out 1069 ms 33980 KB Time limit exceeded
9 Halted 0 ms 0 KB -