最长公共子序列
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
3
-
描述
-
咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列。
tip:最长公共子序列也称作最长公共子串(不要求连续),英文缩写为LCS(Longest Common Subsequence)。其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已知序列的最长公共子序列。-
输入
-
第一行给出一个整数N(0<N<100)表示待测数据组数
接下来每组数据两行,分别为待测的两组字符串。每个字符串长度不大于1000.
输出
- 每组测试数据输出一个整数,表示最长公共子序列长度。每组结果占一行。 样例输入
-
2 asdf adfsd 123abc abc123abc
样例输出
-
3 6
#include<stdio.h> #include <string.h> #define Max_len 1001 int record[Max_len][Max_len]; int len_lcs(char x[], char y[]); int main (){ int N, record; char x[Max_len]; char y[Max_len]; scanf("%d", &N); while(N--){ scanf("%s %s", x, y); record = len_lcs(x, y); printf("%d\n", record); } } int len_lcs(char x[], char y[]){ int n = strlen(x); int m = strlen(y); int i, j; for(i = 0; i < n; i++){ record[i][0] = 0; } for(i = 0; i < m; i++) { record[0][i] = 0; } for(i = 1; i <= n; i++){ for(j = 1; j <= m; j++){ if(x[i - 1] == y[j - 1]){ record[i][j] = record[i - 1][j - 1] + 1; } else if(record[i - 1][j] > record[i][j - 1]){ record[i][j] = record[i - 1][j]; } else{ record[i][j] = record[i][j - 1]; } } } return record[n][m]; }
-
第一行给出一个整数N(0<N<100)表示待测数据组数