The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Thursday, May 3, 2012

Longest Common Sub-sequence 2


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
char a[10],b[10];
int count[10][10],dir[10][10];

void backtrack(int i, int j)
{
if(i!=0 && j!=0)
  {
if(dir[i][j] == 3)
    {
backtrack(i-1,j-1);
printf("%c",a[i-1]);        
    }     
else if(dir[i][j] == 2)
backtrack(i-1,j);
else
backtrack(i,j-1);
  }  
}

int main()
{
                int i,j,k,count1,count2;
                printf("\n Enter 1st string : ");
gets(a);
fflush(stdin);
printf("\n Enter 2nd string : ");
gets(b);
                count1 = strlen(a);
    count2 = strlen(b);

                for(i=1;i<=count1;i++)
                count[i][0]=0;
                for(i=1;i<=count2;i++)
                count[0][i]=0;
                for(i=1;i<=count1;i++)
                {
                                for(j=1;j<=count2;j++)
                                {
                                                if(a[i-1]==b[j-1])
                                                {
                                                                count[i][j]=count[i-1][j-1]+1;
                                                                dir[i][j]=3;
                                                }
                                                else if(count[i][j-1]>=count[i-1][j])
                                                {
                                                                count[i][j]=count[i][j-1];
                                                                dir[i][j]=1;
                                                }
                                                else
                                                {
                                                                count[i][j]=count[i-1][j];
                                                                dir[i][j]=2;
                                                }
                                }
                }
                char temp;
                i=0;
                while(i<count2)
                {
                printf("%c\t",b[i]);
                i++;
    }
                printf("\n");
for(i=1;i<=count1;i++)
                {
printf("%c",a[i-1]);
                                for(j=1;j<=count2;j++)
                                {
                                                if(dir[i][j]==1)
                                                temp='-';
                                                else if(dir[i][j]==2)
                                                temp='|';
                                                else if(dir[i][j]==3)
                                                temp='\\';
                                                printf("%c%d\t",temp,count[i][j]);

                                }
                                printf("\n");
                }             
printf("The LCS is : ");
backtrack(count2,count1);          
getch();
return(0);

}

No comments:

Post a Comment