#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
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++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
588 KB |
Output is correct |
2 |
Correct |
4 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
8 ms |
660 KB |
Output is correct |
8 |
Correct |
28 ms |
588 KB |
Output is correct |
9 |
Correct |
58 ms |
644 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
6 ms |
588 KB |
Output is correct |