#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-Ofast")
#define N 100500
#define NN 205000
#define PB push_back
#define endl '\n'
#define pri(x) cout << x << endl
#define _ << " " <<
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define M ll(1e9 + 7)
#define F first
#define S second
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef short int si;
typedef unsigned long long ull;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
vector <int> vr[30];
int n, m, f[305][305];
string s, t;
int main()
{
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
cin >> n >> m;
cin >> s >> t;
for (int i = 0; i < sz(s); i++)
vr[s[i] - 'a'].PB(i);
int ans = -1;
for (int i = 0; i < 305; i++)
for (int j = 0; j < 305; j++)
f[i][j] = 1e9;
for (int i = 0; i < n; i++)
if (s[i] == t[0])
f[i][0] = 0;
for (int j = 1; j < m; j++)
for (int i = 0; i < n; i++)
{
if (f[i][j - 1] == 1e9) continue;
for (auto it : vr[s[i] - 'a'])
{
int dst = abs(it - i) + 1 + f[i][j - 1];
if (it != 0 && s[it - 1] == t[j])
f[it - 1][j] = min(f[it - 1][j], dst);
if (it + 1 != n && s[it + 1] == t[j])
f[it + 1][j] = min(f[it + 1][j], dst);
}
}
for (int i = 0; i < n; i++)
if (f[i][m - 1] != 1e9 && (ans == -1 || ans > f[i][m - 1]))
{
ans = f[i][m - 1];
}
pri(ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
7 |
Correct |
4 ms |
748 KB |
Output is correct |
8 |
Correct |
29 ms |
748 KB |
Output is correct |
9 |
Correct |
39 ms |
760 KB |
Output is correct |
10 |
Correct |
1 ms |
748 KB |
Output is correct |
11 |
Correct |
2 ms |
748 KB |
Output is correct |