Meaning of the questions:
given two strings, find the longest common subsequence size.
Thinking:
is probably the most classic LCS issues now.
Set \ (X = (x_1, x_2 , ..... x_n) and Y = (y_1, y_2, ..... y_m) \) is the two sequences, the longest common sequences X and Y note as \ (lcs (X, Y) \) , to find out \ (lcs (X, Y) \) is an optimal problem
then we need to be broken down into sub-problems and to find optimal solutions to subproblems: (looking child questions to the launch of the current problems, it is most important to find the equation of state transition step)
1) If \ (Y_M x_n = \) , which is the last element of X and Y are the same as the last element, which shows that the common sub-element must lie sequence. Therefore, just looking now: \ (LCS (. 1-n-X_ {}, {Y_. 1-m}) \)
\ (LCS (. 1-n-X_ {}, {Y_. 1-m}) \) is the original problem a subproblem
2) if \ (! x_n = y_m \) on the back two sub-problems recursively to find: \ (LCS (. 1-n-X_ {}, the Y {m}) and lcs (X_ {n}, Y_ { }. 1-m) \)
\ (LCS (. 1-n-X_ {}, Y_M) \) represents: the longest common sequence may be \ ((x_1, x_2, .... x_ {n-1}) and ( y_1, y_2, ... y_n) \ ) in looking for
\ (lcs (X_n, Y_ { m-1}) \)Represents: the longest common sequence may be \ ((x_1, x_2, .... x_n) and (y_1, y_2, ... y_ { n-1}) \) in looking
finally yielding the following recursive ( recursive) formula: So recurrence can be solved
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#define accept 0
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const int maxn = 1e3+7;
const int maxm = 1e6+7;
const int mod = 1e9+7;
int dp[maxn][maxn];
char a[maxn],b[maxn];
int main(){
while(~scanf("%s%s",a,b)){
memset(dp,0,sizeof(dp));
int lena = strlen(a);
int lenb = strlen(b);
for(int i=1;i<=lena;i++){
for(int j=1;j<=lenb;j++){
if(a[i-1]==b[j-1]){
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[lena][lenb]);
}
}