# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1191492 | SmuggingSpun | Bajka (COCI20_bajka) | C++20 | 34 ms | 328 KiB |
#include<bits/stdc++.h>
#define taskname "B"
using namespace std;
const int lim = 305;
const int INF = 1e9;
template<class T>void minimize(T& a, T b){
if(a > b){
a = b;
}
}
int n, m;
string s, t;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n >> m >> s >> t;
vector<int>dp(n, INF);
for(int i = 0; i < n; i++){
if(s[i] == t[0]){
dp[i] = 0;
}
}
for(int k = 1; k < m; k++){
vector<int>ndp(n, INF);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(s[i] == s[j]){
if(j > 0 && s[j - 1] == t[k]){
minimize(ndp[j - 1], dp[i] + abs(i - j) + 1);
}
if(j + 1 < n && s[j + 1] == t[k]){
minimize(ndp[j + 1], dp[i] + abs(i - j) + 1);
}
}
}
}
swap(dp, ndp);
}
int ans = *min_element(dp.begin(), dp.end());
cout << (ans == INF ? -1 : ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |