#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=305,mod=1e9+7,Inf=1e15;
int dp[N][N],m,n;
string s,t;
main(){
// t=1;
cin >> n >> m;
cin >> s;
s='#'+s;
cin >> t;
t='#'+t;
for(int i=1;i<=m;i++) {
for(int j=0;j<=n+1;j++) {
dp[i][j] = Inf;
}
}
int ans = Inf;
for(int i=1;i<=m;i++) {
for(int j=1;j<=n;j++){
if(s[j]==t[i]) {
dp[i][j] = min(dp[i][j], min(dp[i-1][j-1],dp[i-1][j+1])+1);
if(i==1) dp[i][j] = 0;
for(int k=1;k<=n;k++){
if(s[j]==s[k])dp[i][k] = min(dp[i][k],dp[i][j] + abs(j-k));
}
}
}
if(i==m)
for(int j=1;j<=n;j++){
ans = min(ans,dp[i][j]);
}
}
if(ans==Inf) cout<<-1;
else cout<<ans;
}
Compilation message
bajka.cpp:9:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
9 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
940 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
972 KB |
Output is correct |
2 |
Correct |
7 ms |
936 KB |
Output is correct |
3 |
Correct |
6 ms |
944 KB |
Output is correct |
4 |
Correct |
6 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
17 ms |
1016 KB |
Output is correct |
8 |
Correct |
29 ms |
1012 KB |
Output is correct |
9 |
Correct |
25 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
14 ms |
944 KB |
Output is correct |