feat: updated LCS problem

This commit is contained in:
2025-12-09 21:26:27 -03:00
parent e75daedef5
commit 0951a11635
110 changed files with 201 additions and 30 deletions

View File

@@ -2,16 +2,23 @@
using namespace std;
int solve(string &s1, string &s2, int i = 0, int j = 0)
pair<int, string> solve(string &s1, string &s2, int i = 0, int j = 0)
{
if (i == s1.size() || j == s2.size())
return 0;
return {0, ""};
if (s1[i] == s2[j])
{
return (solve(s1, s2, i + 1, j + 1) + 1);
auto [length, sub] = solve(s1, s2, i + 1, j + 1);
return {1 + length, string(1, s1[i]) + sub};
}
return max(solve(s1, s2, i, j + 1), solve(s1, s2, i + 1, j));
auto [length1, sub1] = solve(s1, s2, i + 1, j);
auto [length2, sub2] = solve(s1, s2, i, j + 1);
if (length1 > length2) {
return {length1, sub1};
}
return {length2, sub2};
}
int main()
@@ -21,7 +28,9 @@ int main()
string s1, s2;
cin >> s1 >> s2;
cout << solve(s1, s2) << endl;
auto [size, sub] = solve(s1, s2);
cout << size << endl;
cout << sub << endl;
return 0;
}

View File

@@ -2,25 +2,50 @@
using namespace std;
int memo[1011][1011];
int solve(string &s1, string &s2, int i = 0, int j = 0)
int main()
{
if (i == s1.size() || j == s2.size()) return 0;
if (memo[i][j] != - 1) return memo[i][j];
if (s1[i] == s2[j]) {
return memo[i][j] = (solve(s1, s2, i + 1, j + 1) + 1);
int n, m;
cin >> n >> m;
string s1, s2;
cin >> s1 >> s2;
/*
let dp[i][j] represent the length of the longest common subsequence using the first i characters
of s1 and the first j characters of s2
if s1[i] == s2[j]
dp[i][j] = dp[i - 1][j - 1] + 1
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
*/
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = 1 + dp[i - 1][j - 1];
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return memo[i][j] = max(solve(s1, s2, i, j + 1), solve(s1, s2, i + 1, j));
}
int main(){
int n, m; cin >> n >> m;
string s1, s2; cin >> s1 >> s2;
memset(memo, -1, sizeof(memo));
cout << solve(s1, s2) << endl;
int row = n, col = m;
string sub = "";
while (dp[row][col]) {
if (s1[row - 1] == s2[col - 1]) {
sub = string(1, s1[row - 1]) + sub;
row--, col--;
} else if (dp[row - 1][col] > dp[row][col - 1]) {
row--;
} else {
col--;
}
}
int size = dp[n][m];
cout << size << endl;
cout << sub << endl;
return 0;
}

View File

@@ -3,15 +3,48 @@
using namespace std;
bool isSubsequence(string &original, string &sub) {
int j = 0;
for (int i = 0; i < original.size() && j < sub.size(); i++) {
if (original[i] == sub[j]) j++;
}
return j == sub.size();
}
int readAns(InStream &stream, string &s1, string &s2) {
int ans = stream.readInt(0, min(s1.size(), s2.size()), "Common subsequence length");
stream.readEoln();
string subsequence;
if (ans == 0) {
stream.readEoln();
subsequence = "";
} else {
subsequence = stream.readToken("[a-z]+");
stream.readEoln();
}
stream.readEof();
quitif(subsequence.size() != ans, _wa, "sequence has length different from informed");
quitif(!isSubsequence(s1, subsequence), _wa, "%s is not a subsequence of %s", subsequence, s1);
quitif(!isSubsequence(s2, subsequence), _wa, "%s is not a subsequence of %s", subsequence, s2);
return ans;
}
int main(int argc, char* argv[]) {
setName("compare two signed int%d's", 8 * int(sizeof(int)));
setName("compare two signed int%d's and check if subsequence given is valid", 8 * int(sizeof(int)));
registerTestlibCmd(argc, argv);
int ja = ans.readInt();
int pa = ouf.readInt();
int n = inf.readInt(), m = inf.readInt();
string s1 = inf.readToken(), s2 = inf.readToken();
if (ja != pa)
quitf(_wa, "expected %d, found %d", ja, pa);
int ja = readAns(ans, s1, s2);
int pa = readAns(ouf, s1, s2);
quitf(_ok, "all answers are correct");
if (ja > pa)
quitf(_wa, "jury has the better answer: jans = %d, pans = %d\n", ja, pa);
else if (ja < pa)
quitf(_fail, "participant has the better answer: jans = %d, pans = %d\n", ja, pa);
quitf(_ok, "answer is correct");
}