Submission #1221518

#TimeUsernameProblemLanguageResultExecution timeMemory
1221518nibertHieroglyphs (IOI24_hieroglyphs)C++20
0 / 100
949 ms2162688 KiB
#include <vector>
#include <algorithm>
using namespace std;

vector<int> ucs(vector<int> A, vector<int> B) {
    int N = A.size(), M = B.size();
    vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));
    
    for (int i = N - 1; i >= 0; --i) {
        for (int j = M - 1; j >= 0; --j) {
            if (A[i] == B[j]) {
                dp[i][j] = 1 + dp[i + 1][j + 1];
            } else {
                dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
            }
        }
    }

    vector<int> lcs;
    int i = 0, j = 0;
    bool multiple = false;

    while (i < N && j < M) {
        if (A[i] == B[j]) {
            lcs.push_back(A[i]);
            i++;
            j++;
        } else if (dp[i + 1][j] == dp[i][j] && dp[i][j + 1] == dp[i][j]) {
            multiple = true;
            break;
        } else if (dp[i + 1][j] == dp[i][j]) {
            i++;
        } else {
            j++;
        }
    }

    if (multiple) return {-1};
    return lcs;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...