#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 |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Incorrect |
2 ms |
1108 KB |
Output isn't correct |
7 |
Correct |
18 ms |
8216 KB |
Output is correct |
8 |
Incorrect |
10 ms |
8188 KB |
Output isn't correct |
9 |
Incorrect |
11 ms |
8188 KB |
Output isn't correct |
10 |
Incorrect |
13 ms |
8188 KB |
Output isn't correct |
11 |
Incorrect |
17 ms |
8916 KB |
Output isn't correct |
12 |
Incorrect |
11 ms |
10452 KB |
Output isn't correct |
13 |
Incorrect |
20 ms |
10452 KB |
Output isn't correct |
14 |
Incorrect |
21 ms |
9428 KB |
Output isn't correct |
15 |
Incorrect |
25 ms |
11092 KB |
Output isn't correct |
16 |
Incorrect |
11 ms |
9032 KB |
Output isn't correct |
17 |
Incorrect |
12 ms |
6920 KB |
Output isn't correct |
18 |
Incorrect |
19 ms |
10580 KB |
Output isn't correct |
19 |
Incorrect |
15 ms |
8188 KB |
Output isn't correct |
20 |
Incorrect |
13 ms |
9556 KB |
Output isn't correct |
21 |
Incorrect |
4 ms |
2388 KB |
Output isn't correct |
22 |
Incorrect |
6 ms |
3924 KB |
Output isn't correct |
23 |
Incorrect |
10 ms |
5332 KB |
Output isn't correct |
24 |
Incorrect |
10 ms |
5588 KB |
Output isn't correct |
25 |
Incorrect |
14 ms |
7508 KB |
Output isn't correct |