题意:求LCS
Sol:经典的 LCS。
if ( i==0 || j==0 ) dp [ i , j ] = 0 ;
else if ( X[ i ] == Y [ j ] ) dp [ i-1 , j-1 ] + 1;
else dp [ i, j ] = max ( dp[ i - 1 , j ] , dp [ i , j-1 ] )
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;
const int maxn = 500 + 10;
int dp[maxn][maxn];
int main()
{
memset(dp,0,sizeof(dp));
int len1,len2;
string str1,str2;
while(cin>>str1>>str2)
{
len1=str1.length();
len2=str2.length();
for(int i=1;i<=len1;i++)
{
for(int j=1;j<=len2;j++)
{
if(str1[i-1]==str2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
printf("%d\n",dp[len1][len2]);
}
return 0;
}