#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 time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
6 |
Correct |
2 ms |
1108 KB |
Output is correct |
7 |
Correct |
18 ms |
8244 KB |
Output is correct |
8 |
Incorrect |
11 ms |
8164 KB |
Output isn't correct |
9 |
Incorrect |
11 ms |
8188 KB |
Output isn't correct |
10 |
Incorrect |
16 ms |
8192 KB |
Output isn't correct |
11 |
Incorrect |
21 ms |
9020 KB |
Output isn't correct |
12 |
Correct |
17 ms |
10456 KB |
Output is correct |
13 |
Incorrect |
21 ms |
10452 KB |
Output isn't correct |
14 |
Incorrect |
18 ms |
9408 KB |
Output isn't correct |
15 |
Incorrect |
26 ms |
11096 KB |
Output isn't correct |
16 |
Incorrect |
14 ms |
9064 KB |
Output isn't correct |
17 |
Incorrect |
12 ms |
6868 KB |
Output isn't correct |
18 |
Incorrect |
24 ms |
10580 KB |
Output isn't correct |
19 |
Incorrect |
17 ms |
8188 KB |
Output isn't correct |
20 |
Incorrect |
13 ms |
9556 KB |
Output isn't correct |
21 |
Incorrect |
5 ms |
2388 KB |
Output isn't correct |
22 |
Incorrect |
8 ms |
3924 KB |
Output isn't correct |
23 |
Incorrect |
9 ms |
5292 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 |