#include <iostream> #include <string> using namespace std;
int lcs(string A,string B); int c[100][100]={0}; int b[100][100]={0}; void image(int i,int j,string &A); int main() { string a,b; cin>>a>>b; if(a.length()>100 || b.length()>100) { cerr<<"Can not get the lcs of the string which length over 100"<<endl; exit(0); } string &aa=a; cout<<"n"<<endl; lcs(a,b); cout<<"n"<<endl; image(a.length(),b.length(),aa); cout<<endl; system("pause"); return 0; }
int lcs(string A,string B) { int mA=A.length(); int nB=B.length();
int len=0; for(int i=1;i<=mA;++i) { for(int j=1;j<=nB;++j) { if(A.at(i-1)==B.at(j-1)) { c[i][j]=c[i-1][j-1]+1; len++; b[i][j]=3; } else if(c[i-1][j]>=c[i][j-1]) { c[i][j]=c[i-1][j]; b[i][j]=1; } else { c[i][j]=c[i][j-1]; b[i][j]=2; } } }
//—–image the road for(int j=1;j<=nB;++j) { cout<<"t"<<B.at(j-1); } cout<<endl; for(int i=1;i<=mA;++i) { cout<<A.at(i-1)<<"t"; for(int j=1;j<=nB;++j) { cout<<b[i][j]<<"t"; } cout<<"n"<<endl; }
return len;
} void image(int i,int j,string &A) {
//—–3:up and left,1:up,2:left if(i==0||j==0) return; if (b[i][j]==3) { cout<<A.at(i-1)<<"t"; image(i-1,j-1,A); }
if(b[i][j]==1) image(i-1,j,A); if(b[i][j]==2) image(i,j-1,A); } |