163. 最長子序列

 avatar
user_6817964
c_cpp
3 years ago
900 B
9
Indexable
int lcs(int*, int*, int, int);
void int_to_array(int*, int, int);
int max(int a, int b);

int max(int a, int b) {
	return (a > b) ? a : b;
}

int lcs(int* text1, int* text2, int m, int n) {
	if (m > 0 && n > 0 && text1[m - 1] == text2[n - 1]) {
		return 1 + lcs(text1, text2, m - 1, n - 1);
	}
	else if (m > 0 && n > 0) {
		return max(lcs(text1, text2, m - 1, n), lcs(text1, text2, m, n - 1));
	}
	else {
		return 0;
	}
}

void int_to_array(int* array1, int value, int index) {
	while (value > 0) {
		*array1 = value % 10;
		value /= 10;
		array1++;
	}
}



int main() {
	int ans = 0;
	int text1[100] = { 0 };
	int text2[100] = { 0 };
	int t1, t2, m, n;

	scanf("%d", &m);
	scanf("%d", &n);
	scanf("%d", &t1);
	scanf("%d", &t2);

	int_to_array(text1, t1, m);
	int_to_array(text2, t2, n);
	ans = lcs(text1, text2, m, n);
	printf("%d", ans);
	return 0;
}
Editor is loading...