This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int inf = 1'000'000'000;
int main()
{
int n, m;
string s, t;
cin >> n >> m >> s >> t;
int dp[n][m];
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
dp[i][j] = inf;
for(int i = 0; i < n; i ++)
if(s[i] == t[0]) dp[i][0] = 0;
for(int j = 0; j + 1 < m; j ++)
for(int i = 0; i < n; i++)
{
for(int k = 0; k < n; k++)
if(s[i] == s[k])
{
if(k > 0 && s[k - 1] == t[j + 1])
dp[k - 1][j + 1] = min(dp[k - 1][j + 1], dp[i][j] + 1 + abs(k - i));
if(k + 1 < n && s[k + 1] == t[j + 1])
dp[k + 1][j + 1] = min(dp[k + 1][j + 1], dp[i][j] + 1 + abs(k - i));
}
}
int ans = inf;
for(int i = 0; i < n; i ++)
ans = min(ans, dp[i][m - 1]);
if(ans == inf)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |