The longest common sub-sequence, print path, pay attention to the format of printing.
The Code is as follows:
# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> using namespace std; char w1 [100] [31], w2 [100] [31]; int dp [101] [101], path [101] [101]; void Print (int sum, int I, int j) // recursively print the path {if (! Sum) return; if (path [I] [j] = 0) {Print (sum-1, I-1, J-1); if (sum! = 1) printf ("% s", w1 [I-1]); else printf ("% s", w1 [I-1]); // No space after the last digit} else if (path [I] [j] = 1) Print (sum, I-1, j ); else if (path [I] [j] = 2) Print (sum, I, J-1);} int main () {# ifdef test freopen ("input.txt ", "r", stdin); # endif int cct1, cct2; while (scanf ("% s", w1 [cct1 = 0])! = EOF) {while (w1 [cct1] [0]! = '#') Scanf ("% s", w1 [++ cct1]); scanf ("% s", w2 [cct2 = 0]); while (w2 [cct2] [0]! = '#') Scanf ("% s", w2 [++ cct2]); for (int I = 0; I <100; ++ I) dp [0] [I] = dp [I] [0] = 0; for (int I = 1; I <= cct1; ++ I) for (int j = 1; j <= cct2; ++ j) {if (! Strcmp (w1 [I-1], w2 [J-1]) {dp [I] [j] = dp [I-1] [J-1] + 1; path [I] [j] = 0;} else {if (dp [I] [J-1] <dp [I-1] [j]) {dp [I] [j] = dp [I-1] [j]; path [I] [j] = 1 ;} else {dp [I] [j] = dp [I] [J-1]; path [I] [j] = 2 ;}}} print (dp [cct1] [cct2], cct1, cct2); puts ("");} return 0 ;}