② The method to solve the problem is generally "dynamic programming", which is also a common method. It is hard to say clearly in a few words. The basic idea is: given a problem, solve it by decomposing sub-problems and then merge them. Usually, many subproblems have high similarity, and dynamic programming tries to solve each subproblem only once, thus reducing the amount of calculation: once the solution of a given subproblem is calculated, it is memorized and stored, so that the table can be directly looked up next time the solution of the same subproblem is needed. -You can search it if you are interested.
③ The demonstration code is as follows:
Program? lcs _ seq
Use? Mathematics;
defined variable
s 1,? s2? :? String;
dp? :? Array? Yes? Array? Yes? Integer;
Me? j,? m,? n? :? Integer;
begin
readln(s 1);
readln(S2);
m? :=? Length (s1); ? n? :=? Length (S2);
Set the length (dp, m+ 1,? n+ 1);
For what? Me? :=? 1? Where to? m? Do what?
For what? j? :=? 1? Where to? n? Do what?
Start?
What if? (me? =? 0)? Or? (j? =? 0)? then what dp[i][j]? :=? 0
Or what? What if? (s 1[i]? =? s2[j])? then what dp[i][j]? :=? dp[i- 1][j- 1]? +? 1
Or what? dp[i][j]? :=? max(dp[i- 1][j],? DP[I][j- 1]);
End;
writeln(DP[M][N]);
End. Run:
1 122 1 12 122
1222 1 122 1 1
seven