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;
vector<vector<int>>freq(26);
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;cin>>n>>m;
string s;
cin>>s;
string t;cin>>t;
for (int i = 0 ;i<n;++i){
freq[s[i]-'a'].push_back(i+1);
}
vector<vector<int64_t>>dp(m+2,vector<int64_t>(n+2,INT_MAX));
for (int i = 0;i<=n;++i){
dp[0][i]=0;
}
for (int i = 1;i<=m;++i){
for (int j = 1;j<=n;++j){
if (s[j-1]==t[i-1]){
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 (auto x:freq[s[j-1]-'a']){
dp[i][x]=min(dp[i][j]+abs(j-x),dp[i][x]);
dp[i][x]=min(dp[i][j]+abs(j-x),dp[i][x]);
}
}
}
}
for (int i = 0;i<=n;++i){
dp[m][0]=min(dp[m][i],dp[m][0]);
}
if (dp[m][0]==INT_MAX)cout<<-1;
else
cout<<dp[m][0];
return 0;}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |