Submission #860456

# Submission time Handle Problem Language Result Execution time Memory
860456 2023-10-13T02:56:44 Z meliodasssf Bajka (COCI20_bajka) C++17
70 / 70
7 ms 1228 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
const int N_MAX = 302;
const int M_MAX = 302;

bool visited[M_MAX][N_MAX];
int L[N_MAX], R[N_MAX];
 
int n, m;
 
string a, b;
 
ll dp[M_MAX][N_MAX];
 
struct path
{
    pair<int,int> s;
    long long len;
};
 
bool operator < (const path &a, const path &b)
{
    return a.len > b.len;
}
 
map <char, int> mp;
 
void dijkstra()
{
    priority_queue <path> pq;
 
    for (int i=1; i<=n; i++)
    {
        if(a[i] == b[1])
        {
            pq.push(path{{1, i}, 0});
        }
    }
    while(pq.empty() == false)
    {
        path p = pq.top();
        pq.pop();
        if(visited[p.s.first][p.s.second] == true) continue;
        visited[p.s.first][p.s.second] = true;
        dp[p.s.first][p.s.second] = p.len;
        if(p.s.first == m) continue;
 
        if(p.s.second > 1 && a[p.s.second - 1] == b[p.s.first + 1] && visited[p.s.first + 1][p.s.second - 1] == false)
        {
            pq.push(path{{p.s.first + 1, p.s.second - 1}, p.len + 1});
        }
        if(p.s.second < n && a[p.s.second + 1] == b[p.s.first + 1] && visited[p.s.first + 1][p.s.second + 1] == false)
        {
            pq.push(path{{p.s.first + 1, p.s.second + 1}, p.len + 1});
        }
        int pos1 = L[p.s.second];
        int pos2 = R[p.s.second];
        if(pos1 != 0 && visited[p.s.first][pos1] == false)
        {
            pq.push(path{{p.s.first, pos1}, p.len + abs(p.s.second - pos1)});
        }
        if(pos2 != 0 && visited[p.s.first][pos2] == false)
        {
            pq.push(path{{p.s.first, pos2}, p.len + abs(p.s.second - pos2)});
        }
    }
}
 
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++)
    {
        if(mp.find(a[i]) != mp.end())
            L[i] = mp[a[i]];
        mp[a[i]] = i;
    }
    mp.clear();
    for(int i = n; i >= 1; i--)
    {
        if(mp.find(a[i]) != mp.end())
            R[i] = mp[a[i]];
        mp[a[i]] = i;
    }
    mp.clear();
    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;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 0 ms 1116 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 3 ms 1116 KB Output is correct
8 Correct 7 ms 1228 KB Output is correct
9 Correct 4 ms 1116 KB Output is correct
10 Correct 1 ms 356 KB Output is correct
11 Correct 2 ms 1116 KB Output is correct