Submission #800533

#TimeUsernameProblemLanguageResultExecution timeMemory
800533fatherofnationRound words (IZhO13_rowords)C++14
24 / 100
26 ms11096 KiB
#include <iostream>
#include <string>
#include <algorithm>
#include<bits/stdc++.h>

using namespace std;

int longestCommonSubsequence(const string& word1, const string& word2) {
    int m = word1.length();
    int n = word2.length();

    // Creating a 2D DP table
    vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

    for (int i = 1; i <= m; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (word1[i - 1] == word2[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

    return dp[m][n];
}

int main() {
    string word1, word2;
    cin >> word1 >> word2;

    // Find the length of the largest common subsequence between
    // roundWord1 and word2, and also between roundWord2 and word1.
    string roundWord1 = word1 + word1;
    string roundWord2 = word2 + word2;
    int result1 = longestCommonSubsequence(roundWord1, word2);
    int result2 = longestCommonSubsequence(roundWord2, word1);

    // Take the maximum of both results to handle the circular nature of round words.
    int result = min(result1, result2);

    cout << result << endl;

    return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...