#include <stdio.h> #include <string.h> #include <iostream>using namespaceStd; int Max (int A ,int B ) { return A>A?A:B;} Char Str1[ the],str2[ the]; int Mark[ the][ the]; int Deal (int I ,int J){ if( I==0&&J==0 ) return 0; if (Mark[I][J]==0) {Deal(I-1,J-1);Printf("%c",Str1[I-1]); } Else if( Mark[I][J]==1) {Deal(I-1,J);Printf("%c",Str1[I-1]); } Else if( Mark[I][J]==2) {Deal(I,J-1);Printf("%c",str2[J-1]); } return 0;} int Main () { int I,J; int Dp[ the][ the]; while (~scanf("%s%s",Str1,str2) ) { int Len1=Strlen(Str1); int Len2=Strlen(str2);Memset(Dp,0 ,sizeof( Dp)); for (I =0;I<=Len1;I++)Mark[I][0] =1; for (I =0;I<=Len2;I++)Mark[0][I] =2; for (I=1;I<=Len1;I + +) { for( J=1;J<=Len2;J + +) { if( Str1[I-1]==str2[J-1]) {Dp[I][J]=Dp[I-1][J-1]+1;Mark[I][J]=0; } Else { //dp[i][j]=max (dp[i-1][j],dp[i][j-1]); if( Dp[I-1][J]>=Dp[I][J-1]) {Dp[I][J]=Dp[I-1][J];Mark[I][J]=1; } Else { Mark[I][J]=2;Dp[I][J]=Dp[I][J-1]; } } } }//printf ("****\n");Deal(Len1,Len2);Printf("\ n"); } return 0;}
HDU 1503 maximum common sub-sequence + output path