最长公共子序列问题:
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
const int N = 100 + 10;
int dp[N][N];
int main(){
string str1, str2;
while(cin>>str1>>str2){
str1 = " " + str1;
str2 = " " + str2;
int n = str1.size() - 1;
int m = str2.size() - 1;
for(int i = 0; i <= n; i++){
for(int j = 0; j <= m; j++){
if(i == 0 || j == 0) dp[i][j] = 0;
else{
if(str1[i] == str2[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
printf("%d\n", dp[n][m]);
}
return 0;
}