Submission #741733

#TimeUsernameProblemLanguageResultExecution timeMemory
741733lalig777Bajka (COCI20_bajka)C++14
20 / 70
1078 ms468 KiB
#include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; int word(string s, string t, int n, int m, int j, int k, bool cambio){ int tiempo=1000000000; if (j==m) return 0; if (k-1>=0 and s[k-1]==t[j]){ int poss=word(s, t, n, m, j+1, k-1, false); tiempo=min(tiempo, poss+1); }if (k+1<n and s[k+1]==t[j]){ int poss=word(s, t, n, m, j+1, k+1, false); tiempo=min(tiempo, poss+1); }for (int l=0; l<n; l++){ if (s[l]==s[k] and l!=k and cambio==false){ int poss=word(s, t, n, m, j, l, true); tiempo=min(tiempo, poss+abs(k-l)); } }return tiempo; } int main(){ int n, m; cin>>n>>m; string s, t; cin>>s>>t; int ans=1e9; for (int i=0; i<n; i++){ if (s[i]==t[0]) ans=min(ans, word(s, t, n, m, 1, i, true)); }if (ans==1e9) cout<<-1<<endl; else cout<<ans<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...