Submission #741733

# Submission time Handle Problem Language Result Execution time Memory
741733 2023-05-14T17:50:34 Z lalig777 Bajka (COCI20_bajka) C++14
20 / 70
1000 ms 468 KB
#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 time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1078 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -