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 <iostream>
#include <vector>
using namespace std;
int main(int argc,const char** argv){
int n,m,ans = 1e9;
cin >> n >> m;
string s,t;
cin >> s >> t;
vector<vector<int>> dp(m,vector<int>(n,1e9));
for(int i = 0;i < m;i++){
vector<int> f; // save pos
for(int j = 0;j < n;j++) if(s[j] == t[i]) f.push_back(j);
for(int pos : f){
if(!i) dp[i][pos] = 0;
else{
if(pos && s[pos - 1] == t[i - 1]){
dp[i][pos] = min(dp[i][pos],dp[i - 1][pos - 1] + 1);
}
if(pos < n - 1 && s[pos + 1] == t[i - 1]){
dp[i][pos] = min(dp[i][pos],dp[i - 1][pos + 1] + 1);
}
}
}
for(int l = 0;l < f.size();l++){
for(int r = 0;r < f.size();r++){
dp[i][f[l]] = min(dp[i][f[l]], abs(f[l] - f[r]) + dp[i][f[r]]);
}
if(i == m - 1){
ans = min(ans,dp[i][f[l]]);
}
}
}
cout << (ans < 1e9 ? ans : -1);
}
Compilation message (stderr)
bajka.cpp: In function 'int main(int, const char**)':
bajka.cpp:26:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int l = 0;l < f.size();l++){
| ~~^~~~~~~~~~
bajka.cpp:27:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int r = 0;r < f.size();r++){
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |