遞迴_最長子序列_不是用遞迴寫的
user_3763047219
c_cpp
3 years ago
971 B
5
Indexable
int lcs(int*, int*, int, int); void int_to_array(int*, int, int); int lcs(int* text1, int* text2, int m, int n) { int count = 0, record = -1; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (text1[i] == text2[j] && record < j) { count++; text2[j] = -1; record = j; } } } return count; } void int_to_array(int* array1, int value, int index) { for (int i = 0; i < index; i++) { array1[i] = value % 10; value = value / 10; } } #include <stdio.h> 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...