UVA_111
This question is not difficult, but it hurts to understand it.
The question indicates the longest common subsequence of two event sequences, and the question indicates the time when each event occurs, therefore, you must first locate the sequence of events based on the time when each event occurs.
For example, if the input is 1 3 4 2, the actual event sequence is 1 4 2 3.
#include<stdio.h>
#include<string.h>
#define MAXD 30
int N, a[MAXD], b[MAXD], f[MAXD][MAXD];
int init()
{
int i, k;
for(i = 1; i <= N; i ++)
{
if(scanf("%d", &k) != 1)
return 0;
b[k] = i;
}
return 1;
}
void solve()
{
int i, j;
memset(f, 0, sizeof(f));
for(i = 1; i <= N; i ++)
for(j = 1; j <= N; j ++)
{
f[i][j] = f[i - 1][j];
if(f[i][j - 1] > f[i][j])
f[i][j] = f[i][j - 1];
if(a[i] == b[j] && f[i - 1][j - 1] + 1 > f[i][j])
f[i][j] = f[i - 1][j - 1] + 1;
}
printf("%d\n", f[N][N]);
}
int main()
{
int i, k;
scanf("%d", &N);
for(i = 1; i <= N; i ++)
{
scanf("%d", &k);
a[k] = i;
}
while(init())
solve();
return 0;
}