遞迴_最長子序列_終於對了

 avatar
user_3763047219
c_cpp
2 years ago
842 B
1
Indexable
Never
#include <stdio.h>

int max(int a, int b) {
	if (a > b) {
		return a;
	}
	else {
		return b;
	}
}

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


void int_to_array(int* array1, int value, int index) {
	while (value > 0) {
		array1[index] = value % 10;
		index--;
		value = value / 10;
	}
}
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;
}