# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1191491 | SmuggingSpun | Bajka (COCI20_bajka) | C++20 | 36 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, 0);
for(char& c : t){
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] == c){
minimize(ndp[j - 1], dp[i] + abs(i - j) + 1);
}
if(j + 1 < n && s[j + 1] == c){
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 - 1);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |