lcs - How to print all possible solutions for Longest Common subsequence -


i want print possible solutions lcs problem.

the 2 strings abcbdab , bdcaba should print following 3 strings: bdab,bcba,bcab.

c global matrix table takes values according algorithm , m, n length of sequences a, b.

but output unexpected.

#include<stdio.h> #include<conio.h> int co=0,m=0,n=0,c[10][10]; char a[10],b[10]; void main() {     int i,j;     clrscr();     printf("enter 2 strings: ");     scanf("%s",a);     scanf("%s",b);     m=strlen(a);     n=strlen(b);     for(i=0;i<=m;i++)     {         for(j=0;j<=n;j++)         {   if(i==0 || j==0)             {                 c[i][j]=0;             }             else if(a[i-1]==b[j-1])             {                 c[i][j]=c[i-1][j-1]+1;             }             else if(c[i-1][j]>=c[i][j-1])             {                 c[i][j]=c[i-1][j];             }             else             {                 c[i][j]=c[i][j-1];             }         }     }     for(i=0;i<=m;i++)     {         for(j=0;j<=n;j++)         {             printf("%d\t",c[i][j]);         }         printf("\n");     }     print(m,n);     getch(); } print(int i,int j) {     if(i==0 || j==0)         return 0;     else if(a[i-1]==b[j-1])     {         print(i-1,j-1);         if(co==c[m][n])         {             co=0;             printf("\n");         }         printf("%c",a[i-1]);         co++;     }     else if(c[i-1][j]==c[i][j-1])     {         print(i-1,j);         print(i,j-1);     }     else if(c[i][j-1]>=c[i-1][j])         print(i,j-1);     else         print(i-1,j);     return; } 

here can find recursive approach of how this: reading out lcss

here code approach in java:

private set<string> lcs(int[][] dp, string fst, string snd, int i, int j) {     set<string> lcss = new hashset<>();      if (i == 0 || j == 0) {         lcss.add("");     } else if (fst.charat(i - 1) == snd.charat(j - 1)) {         (string lcs : lcs(dp, fst, snd, - 1, j - 1)) {             lcss.add(lcs + fst.charat(i - 1));         }     } else {         if (dp[i - 1][j] >= dp[i][j - 1]) {             lcss.addall(lcs(dp, fst, snd, - 1, j));         }          if (dp[i][j - 1] >= dp[i - 1][j]) {             lcss.addall(lcs(dp, fst, snd, i, j - 1));         }     }     return lcss; } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -