# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
800535 | fatherofnation | Round words (IZhO13_rowords) | C++14 | 25 ms | 11092 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 = max(result1, result2);
cout << result << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |