"Template" the longest common sub-sequence
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 6 Chars1[ +],s2[ +];7 intlen1,len2,dp[ +][ +],mark[ +][ +];//If the data is too large, the DP array can consider scrolling the array8 9 voidLCS ()Ten { One inti,j; AMemset (DP,0,sizeof(DP)); - for(i =0; i<=len1;i++) -mark[i][0] =1; the for(i =0; i<=len2;i++) -mark[0][i] =-1; - for(i =1; i<=len1; i++) - { + for(j =1; j<=len2; J + +) - { + if(s1[i-1]==s2[j-1]) A { atDP[I][J] = dp[i-1][j-1]+1; -MARK[I][J] =0; - } - Else if(dp[i-1][j]>=dp[i][j-1]) - { -DP[I][J] = dp[i-1][j]; inMARK[I][J] =1; - } to Else + { -DP[I][J] = dp[i][j-1]; theMARK[I][J] =-1; * } $ }Panax Notoginseng } - } the + voidPrintlcs (intIintj) A { the if(!i &&!)j) + return ; - if(mark[i][j]==0)//of public $ { $Printlcs (I-1, J-1); -printf"%c", s1[i-1]); - } the Else if(mark[i][j]==1) - {WuyiPrintlcs (I-1, j); theprintf"%c", s1[i-1]); - } Wu Else - { AboutPrintlcs (i,j-1); $printf"%c", s2[j-1]); - } -}
Template longest common sub-sequence