#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
string s,t;
vector<vector<int>> dp(305,vector<int> (305,1e18));
signed main(){
cin>>n>>m>>s>>t;
s='.'+s;
t='?'+t;
//dp[i][j] t-iin ehnii i useg s-iin suuliin useg j deer bsn
for(int i=1; i<=n; i++){
if(s[i]==t[1]){
dp[1][i]=0;
}
}
for(int i=2; i<=m; i++){
for(int j=1; j<=n; j++){
// for(int k=
if(j!=1){
if(t[i]==s[j] and t[i-1]==s[j-1]){
dp[i][j]=min(dp[i][j],dp[i-1][j-1]+1);
}
}
if(j!=n){
if(t[i]==s[j] and t[i-1]==s[j+1]){
dp[i][j]=min(dp[i][j],dp[i-1][j+1]+1);
}
}
for(int k=1; k<=n; k++){
if(k==j) continue;
if(s[k]==s[j]) dp[i][k]=min(dp[i][k],dp[i][j]+abs(k-j));
}
}
}
// for(int i=1; i<=m; i++){
// for(int j=1; j<=n; j++){
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
int ans=1e18;
for(int i=1; i<=n; i++){
ans=min(ans,dp[m][i]);
}
if(ans==1e18) cout<<-1<<endl;
else cout<<ans<<endl;
// cout<< (ans==1e18) ? -1 : (ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
1112 KB |
Output is correct |
2 |
Correct |
23 ms |
1116 KB |
Output is correct |
3 |
Correct |
19 ms |
1112 KB |
Output is correct |
4 |
Correct |
26 ms |
1116 KB |
Output is correct |
5 |
Correct |
3 ms |
1112 KB |
Output is correct |
6 |
Correct |
2 ms |
1172 KB |
Output is correct |
7 |
Correct |
25 ms |
1176 KB |
Output is correct |
8 |
Correct |
26 ms |
1116 KB |
Output is correct |
9 |
Correct |
35 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
23 ms |
1184 KB |
Output is correct |