Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

History Grading

111.c

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

int n;
int orig[25];
int dest[25];

int tab[25][25];

#define MAX(a,b) ((a) > (b) ? (a) : (b))
int
lcs(int a, int b) {
	int i,j;
	if (a==0 || b==0) {
		return 0;
	}
	if (tab[a][b]) {
		return tab[a][b];
	}
	if (orig[a] == dest[b]) {
		tab[a][b]=1+lcs(a-1,b-1);
		return tab[a][b];
	}
	i = lcs(a-1,b);
	j = lcs(a,b-1);
	tab[a][b] = MAX(i,j);
	return tab[a][b];
}

int
main(void) {
	int i;
	int a;

	scanf("%d", &n);
	for (i=1; i<=n; i++) {
		scanf("%d", &a);
		orig[a] = i;
	}
	while (1) {
		for (i=1; i<=n; i++) {
			if (scanf("%d", &a) != 1) {
				return 0;
			}
			dest[a] = i;
		}
		memset(tab, 0, sizeof(tab));
		printf("%d\n", lcs(n, n));
	}
}