Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string comman_string(string s1, string s2) {
- int x=s1.length(),y=s2.length(),i,j;
- int a[x+1][y+1];
- memset(a,0,sizeof(a));
- for(i=1;i<=x;i++)
- {
- for(j=1;j<=y;j++)
- {
- if(s1[i-1]==s2[j-1])
- {
- a[i][j]=max(a[i-1][j-1]+s1[i-1],a[i][j-1]);
- }
- else a[i][j]=max(a[i-1][j],a[i][j-1]);
- }
- }
- int index = a[x][y];
- char lcs[index+1];
- lcs[index] = '\0';
- i = m, j = n;
- while (i > 0 && j > 0)
- {
- if (s1[i-1] == s2[j-1])
- {
- lcs[index-1] = s1[i-1]; // Put current character in result
- i--; j--; index--; // reduce values of i, j and index
- }
- else if (a[i-1][j] > a[i][j-1])
- i--;
- else
- j--;
- }
- int index = L[m][n];
- return lcs;
- }
Add Comment
Please, Sign In to add comment