I have already written the part of LCS.
I want to know If I give N(N>3) ,that means how many set of input.
Like this :
Input:
4 ab abc abcd abcde
Output:
3
Just find the longest of those lcs(3 sequences a part)
ab abc abcd->ab->2
abc abcd abcde->abc->3
3>2
My thinking is that every number of set just use the way of 3 sequences then find the bigest one.
But I dont't know how to do it or any better way?
This is a part of my code:
#define EQUAL(x,y,z) ((x)==(y)&&(y)==(z))
int main(){
int set;
int longest;
while (scanf("%d", &set) != EOF){
while (set){
scanf("%s", c1);
set--;
scanf("%s", c2);
set--;
scanf("%s", c3);
set--;
longest = LCS(strlen(c1), strlen(c2), strlen(c3));
}
}
return 0;
}
LCS:
int LCS(int c1_length, int c2_length, int c3_length)
{
memset(lcs, 0, N*N);
int i;
int j;
int k;
for (i = 1; i <= c1_length; i++)
for (j = 1; j <= c2_length; j++)
for (k = 1; k <= c3_length; k++)
{
if (EQUAL(c1[i], c2[j], c3[k]))
lcs[i][j][k] = lcs[i - 1][j - 1][k - 1] + 1;
else
lcs[i][j][k] = max(lcs[i - 1][j][k], lcs[i][j - 1][k], lcs[i][j][k - 1]);
}
return lcs[i - 1][j - 1][k - 1];
}
Thanks everybody~ I have solved this question by using 2d array to store the sequence.