Returns the largest common subsequence of two sequences.
1. Recursion
/*
* Author: dengzhaoqun
* Date: 2011/04/28
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Define length 1000
// Global variables
Char X [1000] = {'/0 '};
Char y [1000] = {'/0 '};
Int * c = NULL;
Char * B = NULL;
// Function declaration
Int * buildc (INT lenx, int leny );
Char * buildb (INT lenx, int leny );
Int lcrrecur (INT endx, int Endy, int leny );
Void trace (INT endx, int Endy, int leny );
Int main ()
{
Int lenx;
Int leny;
Int lcrn; // LCR number
// Get the input
Printf ("input sequence X :");
Scanf ("% s", & X [1]); // endx decrease from lenx to 1
X [0] = 'T'; // make the lenx not be 0
Printf ("input sequence y :");
Scanf ("% s", & Y [1]); // Endy decrease from leny to 1
Y [0] = 'F'; // make the leny not be 0
Lenx = strlen (x)-1;
Leny = strlen (y)-1;
// Built the C [] and B []
C = buildc (lenx, leny );
B = buildb (lenx, leny );
// Call the recurvise Function
Lcrn = lcrrecur (lenx, leny, leny); // endx, Endy, leny
// Output
Printf ("the LCR number is: % d/N", lcrn );
Printf ("the LCR :");
Trace (lenx, leny, leny );
Printf ("/N ");
// Release the memory
Free (C );
Free (B );
Return 0;
}
// --- Begin method buildc ---
Int * buildc (INT lenx, int leny)
{
Int * P = NULL;
Int I, J;
P = (int *) malloc (lenx + 1) * (leny + 1) * sizeof (INT ));
If (null = P)
{
Printf ("memory allocation failed./N ");
Exit (1 );
}
I = 0;
For (I; I <= lenx; I ++)
{
J = 0;
For (J; j <= leny; j ++)
{
P [I * (leny + 1) + J] =-1;
}
}
For (I = 0; I <= lenx; I ++)
P [I * (leny + 1)] = 0;
For (I = 0; I <= leny; I ++)
P [I] = 0;
Return P;
}
// --- End method buildc ---
// --- Begin method buildb ---
Char * buildb (INT lenx, int leny)
{
Char * Ch = NULL;
Int I, J;
Ch = (char *) malloc (lenx + 1) * (leny + 1) * sizeof (char ));
If (null = CH)
{
Printf ("memory allocation failed./N ");
Exit (1 );
}
I = 0;
For (I; I <= lenx; I ++)
{
J = 0;
For (J; j <= leny; j ++)
{
Ch [I * (leny + 1) + J] = 'E'; // indicate error
}
}
Return ch;
}
// --- End method buildb ---
// --- Begin method lcrrecur ---
Int lcrrecur (INT endx, int Endy, int leny)
{
Int tempx, Tempy;
Int * This = & (c [endx * (leny + 1) + Endy]);
If (* This! =-1)
Return (* This );
Else if (X [endx] = Y [Endy])
{
* This = lcrrecur (endx-1, endy-1, leny) + 1;
B [endx * (leny + 1) + Endy] = 'B'; // 'B' represents 'both'
}
Else
{
Tempx = lcrrecur (endx-1, Endy, leny );
Tempy = lcrrecur (endx, endy-1, leny );
If (tempx> Tempy)
{
* This = tempx;
B [endx * (leny + 1) + Endy] = 'X'; // decrease the endx
}
Else
{
* This = Tempy;
B [endx * (leny + 1) + Endy] = 'y'; // decrease the Endy
}
}
Return * this;
}
// --- End method lcrrecur ---
// --- Begin method Trace ---
Void trace (endx, Endy, leny)
{
Char * This = & (B [endx * (leny + 1) + Endy]);
If (endx = 0) | (Endy = 0 ))
Return;
If (* This = 'B ')
{
Trace (endx-1, endy-1, leny );
Printf ("% C", X [endx]);
}
Else if (* This = 'X ')
{
Trace (endx-1, Endy, leny );
}
Else if (* This = 'y ')
{
Trace (endx, endy-1, leny );
}
Else if (* This = 'E ')
{
Printf ("error./N ");
Exit (1 );
}
}
// --- End method Trace ---
2. Iterative Method
/*
* Author: dengzhaoqun
* Date: 2011/04/28
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Define length 1000
// Global variables
Char X [1000] = {'/0 '};
Char y [1000] = {'/0 '};
Int * c = NULL;
Char * B = NULL;
// Function declaration
Int * buildc (INT lenx, int leny );
Char * buildb (INT lenx, int leny );
Void lcriterator (INT endx, int Endy, int leny );
Void trace (INT endx, int Endy, int leny );
Int main ()
{
Int lenx;
Int leny;
// Get the input
Printf ("input sequence X :");
Scanf ("% s", & X [1]); // endx decrease from lenx to 1
X [0] = 'T'; // make the lenx not be 0
Printf ("input sequence y :");
Scanf ("% s", & Y [1]); // Endy decrease from leny to 1
Y [0] = 'F'; // make the leny not be 0
Lenx = strlen (x)-1;
Leny = strlen (y)-1;
// Built the C [] and B []
C = buildc (lenx, leny );
B = buildb (lenx, leny );
// Call the recurvise Function
Lcriterator (lenx, leny, leny );
// Output
Printf ("the LCR number is: % d/N", C [(lenx + 1) * (leny + 1)-1]);
Printf ("the LCR :");
Trace (lenx, leny, leny );
Printf ("/N ");
// Release the memory
Free (C );
Free (B );
Return 0;
}
// --- Begin method buildc ---
Int * buildc (INT lenx, int leny)
{
Int * P = NULL;
Int I, J;
P = (int *) malloc (lenx + 1) * (leny + 1) * sizeof (INT ));
If (null = P)
{
Printf ("memory allocation failed./N ");
Exit (1 );
}
I = 0;
For (I; I <= lenx; I ++)
{
J = 0;
For (J; j <= leny; j ++)
{
P [I * (leny + 1) + J] =-1;
}
}
For (I = 0; I <= lenx; I ++)
P [I * (leny + 1)] = 0;
For (I = 0; I <= leny; I ++)
P [I] = 0;
Return P;
}
// --- End method buildc ---
// --- Begin method buildb ---
Char * buildb (INT lenx, int leny)
{
Char * Ch = NULL;
Int I, J;
Ch = (char *) malloc (lenx + 1) * (leny + 1) * sizeof (char ));
If (null = CH)
{
Printf ("memory allocation failed./N ");
Exit (1 );
}
I = 0;
For (I; I <= lenx; I ++)
{
J = 0;
For (J; j <= leny; j ++)
{
Ch [I * (leny + 1) + J] = 'E'; // indicate error
}
}
Return ch;
}
// --- End method buildb ---
// --- Begin method lcrrecur ---
Void lcriterator (INT endx, int Endy, int leny)
{
Int I, J;
Int tempx, Tempy;
Int * This = NULL;
For (I = 1; I <= endx; I ++)
For (j = 1; j <= Endy; j ++)
{
This = & (c [I * (leny + 1) + J]);
If (X [I] = Y [J])
{
* This = C [(I-1) * (leny + 1) + (J-1)] + 1;
B [I * (leny + 1) + J] = 'B'; // 'B' represent 'both'
}
Else
{
Tempx = C [(I-1) * (leny + 1) + J];
Tempy = C [I * (leny + 1) + (J-1)];
If (tempx> Tempy)
{
* This = tempx;
B [I * (leny + 1) + J] = 'X ';
}
Else
{
* This = Tempy;
B [I * (leny + 1) + J] = 'y ';
}
}
}
}
// --- End method lcrrecur ---
// --- Begin method Trace ---
Void trace (endx, Endy, leny)
{
Char * This = & (B [endx * (leny + 1) + Endy]);
If (endx = 0) | (Endy = 0 ))
Return;
If (* This = 'B ')
{
Trace (endx-1, endy-1, leny );
Printf ("% C", X [endx]);
}
Else if (* This = 'X ')
{
Trace (endx-1, Endy, leny );
}
Else if (* This = 'y ')
{
Trace (endx, endy-1, leny );
}
Else if (* This = 'E ')
{
Printf ("error./N ");
Exit (1 );
}
}
// --- End method Trace ---