Problem link: Online Judge
For some reason I get WA on some test case with this code:
#include <iostream>
#include <string>
using namespace std;
int LCS(string a, string b) {
int alen = a.length();
int blen = b.length();
int dp[alen + 1][blen + 1];
for (int i = 0; i <= alen; i++) {
for (int j = 0; j <= blen; j++) {
if (i == 0 || j == 0) dp[i][j] = 0;
else 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]);
}
}
return dp[alen][blen];
}
int main() {
string x, y;
while (cin >> x) {
cin >> y;
cout << LCS(x, y) << "\n";
}
}