#include <stdio.h> #include <string.h> #include <limits.h> #define MAX 1000 char str1[MAX+1], str2[MAX+1]; // int mem[MAX+1][MAX+1]; int max(int a, int b) { return a > b ? a : b; } // recursive /* int Lcs(char * str1, char * str2, int end1, int end2) // last index { if (end1 == 0 && end2 == 0) { if (str1[end1] == str2[end2]) return 1; else return 0; } if (end1 == 0) { if (str1[end1] == str2[end2]) return 1; else { return Lcs(str1, str2, 0, end2 - 1); } } if (end2 == 0) { if (str1[end1] == str2[end2]) return 1; else { return Lcs(str1, str2, end1 - 1, 0); } } if (str1[end1] == str2[end2]) { return 1 + Lcs(str1, str2, end1 - 1, end2 - 1); } else { return max(Lcs(str1, str2, end1 - 1, end2), Lcs(str1, str2, end1, end2 - 1)); } }*/ // dynamic programming /*int Lcs2(char * str1, char * str2) { if (str1 == NULL || str2 == NULL) return 0; int length1 = strlen(str1), length2 = strlen(str2), i, j; if (str1[0] == str2[0]) mem[0][0] = 1; else mem[0][0] = 0; for (i = 1; i < length1; ++i) { if (str1[i] == str2[0]) mem[i][0] = 1; else mem[i][0] = mem[i-1][0]; } for (j = 1; j < length2; ++j) { if (str1[0] == str2[j]) mem[0][j] = 1; else mem[0][j] = mem[0][j-1]; } for (i = 1; i < length1; ++i) { for (j = 1; j < length2; ++j) { if (str1[i] == str2[j]) { mem[i][j] = mem[i-1][j-1] + 1; } else { mem[i][j] = max(mem[i-1][j], mem[i][j-1]); } } } return mem[length1 - 1][length2 - 1]; } // memory void Init(char * str1, char * str2) { int length1 = strlen(str1), length2 = strlen(str2); int i, j; for (i = 0; i < length1; ++i) { for (j = 0; j < length2; ++j) { mem[i][j] = INT_MIN; } } } int Lcs3(char * str1, char * str2, int end1, int end2) { if (str1 == NULL || str2 == NULL) return 0; if (mem[end1][end2] != INT_MIN) return mem[end1][end2]; if (end1 == 0 && end2 == 0) { if (str1[end1] == str2[end2]) { mem[0][0] = 1; return 1; } else { mem[0][0] = 0; return 0; } } if (end1 == 0) { if (str1[end1] == str2[end2]) { mem[end1][end2] = 1; return 1; } else { mem[end1][end2] = Lcs3(str1, str2, end1, end2 - 1); return mem[end1][end2]; } } if (end2 == 0) { if (str1[end1] == str2[end2]) { mem[end1][end2] = 1; return 1; } else { mem[end1][end2] = Lcs3(str1, str2, end1 - 1, end2); return mem[end1][end2]; } } if (str1[end1] == str2[end2]) { mem[end1][end2] = Lcs3(str1, str2, end1 - 1, end2 - 1) + 1; return mem[end1][end2]; } else { mem[end1][end2] = max(Lcs3(str1, str2, end1, end2 - 1), Lcs3(str1, str2, end1 - 1, end2)); return mem[end1][end2]; } }*/ // space improved dynamic int mem[2][MAX+1]; int Lcs4(char * str1, char * str2) { mem[0][0] = 0; mem[1][0] = 0; int i, j, k1 = 0, k2, length1 = strlen(str1), length2 = strlen(str2); for ( i = 1; i <= length1; ++i) mem[0][i] = 0; for (i = 1; i <= length2; ++i) { k2 = (k1 + 1) % 2; for (j = 1; j <= length1; ++j) { if (str1[j-1] == str2[i-1]) { mem[k2][j] = mem[k1][j-1] + 1; } else { mem[k2][j] = max(mem[k1][j], mem[k2][j-1]); } } k1 = k2; } return mem[k2][length1]; } int main() { int case_num; scanf("%d", &case_num); while (case_num > 0) { scanf("%s", str1); scanf("%s", str2); //printf("%d/n", Lcs(str1, str2, strlen(str1) - 1, strlen(str2) - 1)); // printf("%d/n", Lcs2(str1, str2)); /*Init(str1, str2); printf("%d/n", Lcs3(str1, str2, strlen(str1) - 1, strlen(str2) - 1));*/ printf("%d/n", Lcs4(str1, str2)); --case_num; } return 0; } /*int main() { int case_num; scanf("%d", &case_num); while (scanf("%s%s", str1, str2) != EOF) { //printf("%d/n", Lcs(str1, str2, strlen(str1) - 1, strlen(str2) - 1)); // printf("%d/n", Lcs2(str1, str2)); Init(str1, str2); printf("%d/n", Lcs3(str1, str2, strlen(str1) - 1, strlen(str2) - 1)); printf("%d/n", Lcs4(str1, str2)); --case_num; } return 0; }*/