#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 1010
int max(int a, int b )
{
if ( a > b )
return a;
else
return b;
};
char s1[N],s2[N];
int dp[N][N];
int main()
{
while(gets(s1)&&gets(s2))
{
int len1=strlen(s1),len2=strlen(s2);
memset(dp,0,sizeof(dp));
for ( int i = 0 ; i < len1 ; i++ )
{
for (int j = 0 ;j < len2 ; j++ )
{
dp[i+1][j+1] = max ( dp[i+1][j] ,dp[i][j+1] );
if ( s1[i] == s2[j] )
{
dp[i+1][j+1] = max ( dp[i][j] + 1 , dp[i+1][j+1] );
}
else
{
dp[i+1][j+1] = max ( dp[i][j+1] , dp[i+1][j] );
}
}
}
printf ( "%d\n" , dp[len1][len2] );
}
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 1010
int max(int a, int b )
{
if ( a > b )
return a;
else
return b;
};
char s1[N],s2[N];
int dp[N][N];
int main()
{
while(gets(s1)&&gets(s2))
{
int len1=strlen(s1),len2=strlen(s2);
memset(dp,0,sizeof(dp));
for ( int i = 0 ; i < len1 ; i++ )
{
for (int j = 0 ;j < len2 ; j++ )
{
dp[i+1][j+1] = max ( dp[i+1][j] ,dp[i][j+1] );
if ( s1[i] == s2[j] )
{
dp[i+1][j+1] = max ( dp[i][j] + 1 , dp[i+1][j+1] );
}
else
{
dp[i+1][j+1] = max ( dp[i][j+1] , dp[i+1][j] );
}
}
}
printf ( "%d\n" , dp[len1][len2] );
}
}
状态dp[i][j],以及他的转移。
以及如何来读取输入,get能够读取空格。