답안 #918890

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
918890 2024-01-30T16:35:44 Z vjudge1 Bajka (COCI20_bajka) C++17
20 / 70
1 ms 1116 KB
#include "bits/stdc++.h"
using namespace std;
/*string a;
long long Recursi_n(string Actual, string Objetivo, long long Minutos, long long Posici_n){
    if(Actual == Objetivo) return Minutos;
    vector<long long> Posibles;
    if(Minutos == 0) for(long long i = 0; i < a.size(); i++) if(Objetivo[0] == a[i]) Posibles.push_back(Recursi_n(Actual + a[i], Objetivo, Minutos, i));
    else {
        if(Posici_n < a.size() - 1 and a[Posici_n + 1] == Objetivo[Actual.size()]) Posibles.push_back(Actual + a[Posici_n + 1], Objetivo, Minutos + 1, Posici_n + 1);
        if(Posici_n > 0 and a[Posici_n - 1] == Objetivo[Actual.size()]) Posibles.push_back(Actual + a[Posici_n - 1], Objetivo, Minutos + 1, Posici_n - 1);
        for(long long i = 0; i < a.size(); i++) if(i != Posici_n and a[i] == a[Posici_n]) Posibles.push_back(Actual, Objetivo, Minutos + abs(Posici_n - i), i);
    }
    if(Posibles.empty()) return -1;
    else {
        sort(Posibles.begin(), Posibles.end());
        for(long long i = 0; i < Posibles.size(); i++) if(Posibles[i] != -1) return Posibles[i];
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long n, m;
    string b;
    cin>>n>>m>>a>>b;
    if(n == 1){
        if(m == 1 and a[0] == b[0]) cout<<0;
        else cout<<-1;
        return 0;
    }
    cout<<Recursi_n("", b, 0, -1);
    return 0;
}*/
string a, b;
vector< vector<long long> > Memorizaci_n;
long long Resolver(long long i, long long j){
    //cout<<i<<" "<<j<<"\n";
    long long Retorno = 2222222222222222;
    if(Memorizaci_n[i][j] != -2) return Memorizaci_n[i][j];
    else if(j >= b.size() - 1) return 0;
    Memorizaci_n[i][j] = -1;
    if(i == 0 and a[i + 1] == b[j + 1]){
        long long A = Resolver(i + 1, j + 1);
        if(A == -1) A = 2222222222222221;
        Retorno = min(Retorno, A + 1);
    } else if(i == a.size() - 1 and a[i - 1] == b[j + 1]){
        long long A = Resolver(i - 1, j + 1);
        if(A == -1) A = 2222222222222221;
        Retorno = min(Retorno, A + 1);
    } else {
        if(a[i + 1] == b[j + 1]){
            long long A = Resolver(i + 1, j + 1);
            if(A == -1) A = 2222222222222221;
            Retorno = min(Retorno, A + 1);
        }
        if(a[i - 1] == b[j + 1]){
            long long A = Resolver(i - 1, j + 1);
            if(A == -1) A = 2222222222222221;
            Retorno = min(Retorno, A + 1);
        }
    }
    for(long long k = i + 1; k < a.size(); k++){
        if(a[k] == a[i]){
            long long A = Resolver(k, j);
            if(A == -1) A = 2222222222222222 - abs(i - k);
            Retorno = min(Retorno, A + abs(i - k));
            break;
        }
    }
    for(long long k = i - 1; k > -1; k--){
        if(a[k] == a[i]){
            long long A = Resolver(k, j);
            if(A == -1) A = 2222222222222222 - abs(i - k);
            Retorno = min(Retorno, A + abs(i - k));
            break;
        }
    }
    if(Retorno == 2222222222222222) return Memorizaci_n[i][j] = -1;
    else return Memorizaci_n[i][j] = Retorno;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long n, m;
    cin>>n>>m>>a>>b;
    if(n == 1 and a == b){
        cout<<0;
        return 0;
    } else if(n == 1 and a != b){
        cout<<-1;
        return 0;
    }
    Memorizaci_n.assign(n, vector<long long>(m, -2));
    long long Respuesta = 2222222222222222;
    for(long long i = 0; i < n; i++) if(a[i] == b[0]) Respuesta = min(Respuesta, Resolver(i, 0));
    cout<<Respuesta;
    return 0;
}

Compilation message

bajka.cpp: In function 'long long int Resolver(long long int, long long int)':
bajka.cpp:39:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     else if(j >= b.size() - 1) return 0;
      |             ~~^~~~~~~~~~~~~~~
bajka.cpp:45:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     } else if(i == a.size() - 1 and a[i - 1] == b[j + 1]){
      |               ~~^~~~~~~~~~~~~~~
bajka.cpp:61:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for(long long k = i + 1; k < a.size(); k++){
      |                              ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 544 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 960 KB Output is correct
2 Incorrect 1 ms 1116 KB Output isn't correct
3 Halted 0 ms 0 KB -