题目链接:​​http://poj.org/problem?id=1458​

dp[i][j]表示串s1前i字符和串s2前j字符的最长公共子序列。

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

int dp[1005][1005];
char s1[1005];
char s2[1005];

int main()
{
while (cin>>s1)
{
cin >> s2;
int len1 = strlen(s1);
int len2 = strlen(s2);


for (int i = 1; i <= len1; i++)
{
for (int j = 1; j <= len2; j++)
{
if (s1[i - 1] == s2[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[len1][len2] << endl;
}

return 0;
}