遞迴_最長子序列_終於對了
user_3763047219
c_cpp
01/05/2023 4:45 AM
1.1 KB
18
Indexable
#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;
}Editor is loading...