DevJourney · code I wrote while learning
main.cpp
847 B#include <iostream>
#include <string>
using namespace std;
int main()
{
string a, b;
cin >> a >> b;
int n = a.size(), m = b.size();
int dp[100][100] = {0}; // assuming strings < 100
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i - 1] == b[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
cout << dp[n][m] << endl;
string lcs;
int i = n, j = m;
while (i > 0 && j > 0)
{
if (a[i - 1] == b[j - 1])
{
lcs += a[i - 1];
i--;
j--;
}
else if (dp[i - 1][j] > dp[i][j - 1])
i--;
else
j--;
}
for (int k = lcs.size() - 1; k >= 0; k--)
cout << lcs[k];
cout << endl;
}