#include <bits/stdc++.h> //hi
using namespace std;
#define endl '\n'
int n,m;
int A[100010],B[100010];
int L[100010],R[100010];
void in()
{
cin >> n >> m;
for (int i=1; i<=n; i++)
{
char x;
cin >> x;
A[i] = x - 'a' + 1;
}
for (int i=1; i<=n; i++)
{
char x;
cin >> x;
B[i] = x - 'a' + 1;
}
// for (int i=1; i<=n; i++) cout << A[i] << ' ' ; cout << endl;
// for (int i=1; i<=n; i++) cout << B[i] << ' ' ; cout << endl;
}
void set_up()
{
int tmp[100];
memset(tmp,-1,sizeof(tmp));
for (int i=1; i<=n; i++)
{
if (tmp[A[i]] != -1) L[i] = tmp[A[i]];
tmp[A[i]] = i;
}
memset(tmp,-1,sizeof(tmp));
for (int i=n; i>=1; i--)
{
if (tmp[A[i]] != -1) R[i] = tmp[A[i]];
tmp[A[i]] = i;
}
// for (int i=1; i<=n; i++) cout << L[i] << ' ' ; cout << endl;
// for (int i=1; i<=n; i++) cout << R[i] << ' ' ; cout << endl;
}
long long dp[1010][1010];
bool visited[1010][1010];
struct State
{
int pref;
int pos;
};
struct Path
{
State 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{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});
int pos1 = L[p.s.pos];
int pos2 = R[p.s.pos];
if(pos1 != 0 && visited[p.s.pref][pos1] == false)
pq.push(Path{State{p.s.pref, pos1}, p.len + abs(p.s.pos - pos1)});
if(pos2 != 0 && visited[p.s.pref][pos2] == false)
pq.push(Path{State{p.s.pref, pos2}, p.len + abs(p.s.pos - pos2)});
}
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
in();
set_up();
dijkstra();
long long res = LLONG_MAX;
for (int i=1; i<=n; i++)
{
if(visited[m][i] == true)
{
res = min(res, dp[m][i]);
}
}
if(res == LLONG_MAX) cout << -1;
else cout << res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
3 ms |
6744 KB |
Output is correct |
8 |
Correct |
7 ms |
6748 KB |
Output is correct |
9 |
Correct |
4 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
3 ms |
6748 KB |
Output is correct |