zoj 1733
#include <iostream>
#include <string.h>
#define N 1005
using namespace std ;
char s1[N],s2[N];
int dp[N][N];
int max(int a,int b)
{ return a>b ? a : b ; }
void f(int n,int m)
{
int i,j;
for (i=0; i<n; i++) dp[i][0]=0;
for (j=0; j<m; j++) dp[0][j]=0;
for (i=1;i<=n; i++)
for (j=1;j<=m; 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]);
}
int main()
{
int i,j,m,n;
while ( cin>>s1>>s2 )
{ n=strlen(s1);
m=strlen(s2);
f(n,m);
cout << dp[n][m] << endl ;
}
}
1458 poj--zoj 1733---------------动态规划DP,布布扣,bubuko.com
时间: 2024-10-12 21:29:34