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;
#define ll long long
int const N=305;
int const inf=1e9+7;
int n,m;
string s,t;
int dp[N][N];
int main(){
cin>>n>>m>>s>>t;
for(int j=1;j<m;j++)
for(int i=0;i<n;i++){
dp[i][j]=inf;
for(int k=0;k<n;k++){
if(s[k]!=s[i])continue;
if(k>0 && s[k-1]==t[m-j])
dp[i][j]=min(dp[i][j],abs(i-k)+1+dp[k-1][j-1]);
if(k+1<n && s[k+1]==t[m-j])
dp[i][j]=min(dp[i][j],abs(i-k)+1+dp[k+1][j-1]);
}
}
int ans=inf;
for(int i=0;i<n;i++)
if(s[i]==t[0])
ans=min(ans,dp[i][m-1]);
if(ans==inf)
ans=-1;
cout<<ans<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |