#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 path
{
pair<int,int> s;
long long len;
};
bool operator < (const path &a, const path &b)
{
return a.len > b.len;
}
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;
}
Compilation message
bajka.cpp: In function 'void dijkstra()':
bajka.cpp:33:12: error: 'A' was not declared in this scope
33 | if(A[i] == B[1])
| ^
bajka.cpp:33:20: error: 'B' was not declared in this scope
33 | if(A[i] == B[1])
| ^
bajka.cpp:42:12: error: 'visited' was not declared in this scope
42 | if(visited[p.s.first][p.s.second] == true) continue;
| ^~~~~~~
bajka.cpp:43:9: error: 'visited' was not declared in this scope
43 | visited[p.s.first][p.s.second] = true;
| ^~~~~~~
bajka.cpp:47:30: error: 'A' was not declared in this scope
47 | 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)
| ^
bajka.cpp:47:51: error: 'B' was not declared in this scope
47 | 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)
| ^
bajka.cpp:51:30: error: 'A' was not declared in this scope
51 | 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)
| ^
bajka.cpp:51:51: error: 'B' was not declared in this scope
51 | 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)
| ^
bajka.cpp:55:20: error: 'L' was not declared in this scope
55 | int pos1 = L[p.s.second];
| ^
bajka.cpp:56:20: error: 'R' was not declared in this scope
56 | int pos2 = R[p.s.second];
| ^
bajka.cpp: In function 'int main()':
bajka.cpp:79:12: error: 'mp' was not declared in this scope; did you mean 'dp'?
79 | if(mp.find(a[i]) != mp.end())
| ^~
| dp
bajka.cpp:80:13: error: 'L' was not declared in this scope
80 | L[i] = mp[a[i]];
| ^
bajka.cpp:81:9: error: 'mp' was not declared in this scope; did you mean 'dp'?
81 | mp[a[i]] = i;
| ^~
| dp
bajka.cpp:83:5: error: 'mp' was not declared in this scope; did you mean 'dp'?
83 | mp.clear();
| ^~
| dp
bajka.cpp:87:13: error: 'R' was not declared in this scope
87 | R[i] = mp[a[i]];
| ^
bajka.cpp:91:5: error: 'Dijkstra' was not declared in this scope; did you mean 'dijkstra'?
91 | Dijkstra();
| ^~~~~~~~
| dijkstra
bajka.cpp:94:12: error: 'visited' was not declared in this scope
94 | if(visited[m][i] == true)
| ^~~~~~~