Description:
Returns two strings and calculates the length of the longest common subsequence of the two strings.
Analysis:
ClassicDynamic PlanningQuestion.
Set string to S, T. F [I] [J] indicates the longest length of a common subsequence consisting of the first I letters of S and the first J letters of T.
State transition equation:
F [I] [J] = f [I-1] [J-1] + 1 (s [I] = T [J])
F [I] [J] = max {f [I-1] [J], F [I] [J-1]}
/* Zju1733 common subsequence */# include
# Include
# Define n 205 # define max (A, B) (a)> (B )? (A) :( B) # define CLR (a) memset (A, 0, sizeof (A) int LCS (char s [], char T []) {int I, j; int f [N] [N] = {0}; for (I = 0; s [I]; I ++) {for (j = 0; t [J]; j ++) {If (s [I] = T [J]) f [I + 1] [J + 1] = f [I] [J] + 1; else f [I + 1] [J + 1] = max (F [I + 1] [J], F [I] [J + 1]);} return f [I] [J];} int main () {char s [N], t [N]; while (scanf ("% S % s", S, t )! = EOF) {printf ("% d/N", LCS (S, T);} return 0 ;}