Submission #806685

#TimeUsernameProblemLanguageResultExecution timeMemory
806685Tunglam07Bajka (COCI20_bajka)C++17
70 / 70
24 ms980 KiB
#include<bits/stdc++.h> using namespace std; long long n, m, minn = INT_MAX, pre[301][301]; string s, t; vector<int> ch[27]; long long backtrack(int id, int pos) { if(pre[id][pos]) { return pre[id][pos]; } if(pos == m) { return 0; } long long ans = INT_MAX; for(int i : ch[s[id] - 96]) { if(i < n - 1 && s[i + 1] == t[pos]) { ans = min(ans, backtrack(i + 1, pos + 1) + abs(id - i) + 1); } if(i > 0 && s[i - 1] == t[pos]) { ans = min(ans, backtrack(i - 1, pos + 1) + abs(id - i) + 1); } } pre[id][pos] = ans; return ans; } int main() { cin >> n >> m; cin>> s; cin>> t; for(int i = 0; i < n; i++) { ch[s[i] - 96].push_back(i); } for(int c : ch[t[0] - 96]) { minn = min(minn, backtrack(c, 1)); } if(minn < INT_MAX) { cout << minn; return 0; } cout << -1; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...