公共子序列
多组输入的最长公共子序列。
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <string>
- using namespace std;
- //Mystery_Sky
- //
- #define M 1000
- string s1, s2;
- int f[M][M], len1, len2;
- int main() {
- while(cin >> s1 >> s2) {
- memset(f, 0, sizeof(f));
- len1 = s1.size(), len2 = s2.size();
- for(int i = 1; i <= len1; i++) {
- for(int j = 1; j <= len2; j++) {
- if(s1[i-1] == s2[j-1]) f[i][j] = max(f[i][j], f[i-1][j-1]+1);
- else f[i][j] = max(f[i-1][j], f[i][j-1]);
- }
- }
- printf("%d\n", f[len1][len2]);
- s1.clear(), s2.clear();
- }
- return 0;
- }