Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<bits/stdc++.h>
  3.  
  4.  
  5.  
  6.  
  7. int llis(char arr[],int n, int *max)
  8. {
  9.  
  10. if (n == 1)
  11. return 1;
  12.  
  13.  
  14. int r;
  15. int loflis = 1;
  16.  
  17.  
  18. for (int i = 1; i < n; i++)
  19. {
  20. r = llis(arr, i, max);
  21. if (arr[i-1] < arr[n-1] && r + 1 > loflis)
  22. loflis = r + 1;
  23. }
  24.  
  25.  
  26. if (*max < loflis)
  27. *max = loflis;
  28.  
  29.  
  30. return loflis;
  31. }
  32.  
  33. int lis(char arr[], int n)
  34. {
  35.  
  36. int max = 1;
  37.  
  38.  
  39. llis( arr, n, &max );
  40.  
  41.  
  42. return max;
  43. }
  44.  
  45. int max(int a, int b)
  46. {
  47. return (a > b)? a : b;
  48. }
  49.  
  50.  
  51. int lcs( char *X, char *Y, int m, int n )
  52. {
  53. if (m == 0 || n == 0)
  54. return 0;
  55. if (X[m-1] == Y[n-1])
  56. return 1 + lcs(X, Y, m-1, n-1);
  57. else
  58. return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
  59. }
  60.  
  61.  
  62. int main()
  63. {
  64. char arr[] = { 1, 2, 1, 3, 9, 5, 4, 9, 10 };
  65. char arr1[]={ 'A', 'B', 'C', 'I', 'X', 'P'};
  66.  
  67. int n = sizeof(arr)/sizeof(arr[0]);
  68.  
  69. printf("lis length is of arr : %d\n",lis( arr, n ));
  70. printf("lis length is of arr1 : %d\n",lis( arr1, n ));
  71.  
  72.  
  73.  
  74.  
  75. char X[] = "AGGTAB";
  76. char Y[] = "GXTXAYB";
  77.  
  78. int m = strlen(X);
  79. int k = strlen(Y);
  80.  
  81. printf("Length of LCS is %d \n", lcs( X, Y, m, k ) );
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement