就是求最长公共子序列。

###Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//By Brickgao
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
#define out(v) cerr << #v << ": " << (v) << endl
#define SZ(v) ((int)(v).size())
const int maxint = -1u>>1;
template <class T> bool get_max(T& a, const T &b) {return b > a? a = b, 1: 0;}
template <class T> bool get_min(T& a, const T &b) {return b < a? a = b, 1: 0;}

char a[1100], b[1100];
int c[1100][1100];
int la, lb;

int main() {
while(gets(a) && gets(b)) {
la = strlen(a);
lb = strlen(b);
memset(c, 0, sizeof(c));
for(int i = 0; i < la; i ++) {
for(int j = 0; j < lb; j ++) {
if(a[i] == b[j]) {
c[i + 1][j + 1] = c[i][j] + 1;
}
else {
c[i + 1][j + 1] = max(c[i][j + 1], c[i + 1][j]);
}
}
}
cout << c[la][lb] << endl;
}
return 0;
}