Exemple #1
0
int LCSLength (char* a, char * b, int m, int n){


        if(m==0 || n== 0){
            return 0;
        }
        else if(*(a+m)==*(b+n)){
            return 1+ LCSLength(a,b,m-1,n-1);
        }
        else{

            return max(LCSLength(a,b,m,n-1),LCSLength(a,b,m-1,n));
        }
}
int main()
{
    char* XArray = " ABCBDAB";
    char* YArray = " BDCABA";
    int XLength = strlen(XArray);
    int YLength = strlen(YArray);
    int **MaxLenghtMetrix = (int **)malloc(XLength*sizeof(int *));
    int i;
    for(i = 0; i < XLength; ++i)
    {
        MaxLenghtMetrix[i] = (int *)malloc(YLength*sizeof(int));
    }
    int **SolvedNumberMetrix = (int **)malloc(XLength*sizeof(int *));
    for(i = 0; i < XLength; ++i)
    {
        SolvedNumberMetrix[i] = (int *)malloc(YLength*sizeof(int));
    }
    LCSLength(XLength-1,YLength-1, XArray, YArray,
               MaxLenghtMetrix, SolvedNumberMetrix);
    printf("================\n");
    printf("MaxLenghtMetrix:\n");
    print_array(MaxLenghtMetrix,XLength,YLength);
    printf("================\n");
    printf("SolvedNumberMetrix:\n");
    print_array(SolvedNumberMetrix,XLength,YLength);
    LCS(XLength-1,YLength-1,XArray,SolvedNumberMetrix);
    return 0;
}
Exemple #3
0
int main(){
  int t;
  char str1[40002], str2[40002];

  scanf("%d",&t);
  while(t--){
    scanf("%s%s",&str1,&str2);
    if(LCSLength(str1,str2) == 1)
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}
Exemple #4
0
int main(int argc, char **argv)
{
    char x[MAXLEN] = {"ABCBDAB"};
    char y[MAXLEN] = {"BDCABA"};
    int b[MAXLEN][MAXLEN];
    int c[MAXLEN][MAXLEN];
    int m, n;
    
    m = strlen(x);
    n = strlen(y);
    
    LCSLength(x, y, m, n, c, b);
    PrintLCS(b, x, m, n);
    
    PrintArray();
    return 0;
}
Exemple #5
0
int main()
{

    char * str1 = "AABCDEAB";
    char * str2 = "ABCDEBABD";

    int m,n;
    int ans;

    m = strlen(str1);
    n = strlen(str2);


    ans = LCSLength(str1,str2,m,n);

    printf("LCS(Recursion) of %s and %s : %d\n",str1,str2,ans);

    ans = LCS_DP(str1,str2,m,n);

    printf("LCS(DP) of %s and %s : %d\n",str1,str2,ans);


    return 0;
}