#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,1e12));
signed main(){
cin>>n>>m>>s>>t;
s='.'+s;
t='.'+t;
for(int i=1; i<=n; i++){
if(s[i]==t[1]){
dp[1][i]=0;
}
}
//dp[i][j] - ehnii i useg bas suuliin index ni j
for(int i=2; i<=m; i++){
for(int j=1; j<=n; j++){
if(t[i]==s[j]){
for(int k=1; k<=n; k++){
// if(s[i]
if(k==j) continue;
dp[i][j]=min(dp[i][j],dp[i-1][k]+abs(k-j));
}
for(int k=j, idx=i; k<=n and idx<=m; k++, idx++){
if(t[idx]==s[k]){
dp[idx][k]=min(dp[idx][k],dp[i][j]+k-j+1);
}
else break;
}
for(int k=j, idx=i; k>=1 and idx<=m; k--, idx++){
if(t[idx]==s[k]){
dp[idx][k]=min(dp[idx][k],dp[i][j]+abs(k-j)+1);
}
else break;
}
}
}
}
int ans=1e12;
// for(int i=1; i<=m; i++){
// for(int j=1; j<=n; j++){
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
for(int i=1; i<=n; i++){
ans=min(ans,dp[m][i]);
}
if(ans>1e9) cout<<-1<<endl;
else cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 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 |
Incorrect |
5 ms |
1116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |