Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- char X[100],Y[100],LCS[100];
- scanf("%s",X);
- scanf("%s",Y);
- int n1=strlen(X),n2=strlen(Y);
- int i,j,Table[n1+1][n2+1],k=0;
- char direction[n1+1][n2+1];
- for(i=0;i<=n1;i++)
- {
- for(j=0;j<=n2;j++)
- {
- if(i==0 || j==0)
- {
- Table[i][j]=0;
- direction[i][j]='N';
- }
- else if(X[i-1]==Y[j-1])
- {
- Table[i][j]=Table[i-1][j-1]+1;
- direction[i][j]='D';
- }
- else
- {
- if(Table[i-1][j]>=Table[i][j-1])
- {
- Table[i][j]=Table[i-1][j];
- direction[i][j]='U';
- }
- else
- {
- Table[i][j]=Table[i][j-1];
- direction[i][j]='L';
- }
- }
- }
- }
- for(j=n2,i=n1;j>=0,i>=0;j,i)
- {
- if(Table[i][j]==0)break;
- else if(direction[i][j]=='D')
- {
- LCS[k]=Y[j-1];
- i--,j--,k++;
- }
- else if(direction[i][j]=='L')j--;
- else if(direction[i][j]=='U')i--;
- }
- cout<<endl<<k<<endl;
- for(i=k-1;i>=0;i--) printf("%c",LCS[i]);
- cout<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment