Just For Dream

###### uva：111 - History Grading

LCS问题：动规方程：dp [i][j] = dp[i-1][j-1] +1 (numx[i] = numy[j]) || max{ dp[i-1] [j] , dp[i][j-1] } || 0 (i=0 || j =0) ;

#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std ;

const int maxn = 25 ;

int n;
int numx[maxn] ;
int numy[maxn] ;
int dp[maxn][maxn] ;

int main()
{
//freopen("data.in" , "r" , stdin) ;

int i ;
int j ;
int x ;

cin>>n;

for(i = 1 ; i <= n ; i ++)
{
cin>>x ;
numx[x] = i ;
}

while(cin>>x)
{
numy[x] = 1 ;

for(j = 2 ; j <= n ; j ++)
{
cin>>x ;
numy[x] = j ;
}

memset(dp , 0 , sizeof(dp)) ;

for(i = 1 ; i <= n ; i ++)
{
for(j = 1 ; j <= n ; j ++)
{
if(numx[i]==numy[j])
dp[i][j] = dp[i-1][j-1] + 1 ;
else
dp[i][j] = dp[i-1][j] > dp[i][j-1] ? dp[i-1][j] : dp[i][j-1] ;
}
}

printf("%d\n" , dp[n][n]) ;
}

return 0 ;
}


#include<iostream>
#include<string.h>
#include<stdio.h>

using namespace std ;

const int maxn = 1005 ;

char numa[maxn] ;
char numb[maxn] ;
int  num[maxn][maxn] ;

int lena ;
int lenb ;

int dp(int , int ) ;
int main()
{
//freopen("data.in" , "r" , stdin) ;

while(1)
{
cin.getline(numa , sizeof(numa)) ;
if(!cin)
break ;
cin.getline(numb  , sizeof(numb)) ;

lena = strlen(numa ) ;
lenb = strlen(numb ) ;

memset(num , -1 , sizeof(num)) ;

dp(lena - 1 , lenb - 1) ;

printf("%d\n" , num[lena-1][lenb-1]) ;
}
return 0 ;
}

int dp(int n , int m)
{
int & ans = num[n][m] ;

if(n < 0 || m < 0)
return 0 ;

if(ans >= 0)
{
return ans ;
}

if(numa[n]==numb[m])
{
ans = dp(n - 1 , m - 1) + 1 ;
}

else if(dp(n-1 , m) >= dp(n , m - 1))
{
ans = dp(n-1 , m) ;
}
else ans = dp(n , m - 1) ;

return ans ;
}

#### UVa 111 - History Grading

2012-08-19 00:03:02

#### UVa 111 - History Grading

2014-09-15 10:53:12

#### History Grading（uva 111）

2016-05-14 10:38:03

#### UVA-111 History Grading

2014-07-29 19:29:54

#### UVA 111 History Grading （最长公共子序列）

2016-04-20 19:21:22

#### [UVA 111] History Grading

2012-01-29 17:54:11

#### UVA - 111 History Grading

2013-09-06 13:13:16

#### uva 111 History Grading

2014-05-13 19:52:07

#### Uva 111 – History Grading

2011-03-27 22:55:00

#### UVA 111 History Grading

2013-11-22 13:14:40

## 不良信息举报

uva：111 - History Grading