Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define MAX(A,B) (A<B) ? B : A
  6.  
  7. typedef struct dcsn {
  8. char common_elem;
  9. struct dcsn* next_dcsn;
  10. int length;
  11. } dcsn_t;
  12.  
  13. dcsn_t*
  14. make_dcsn(void)
  15. {
  16. dcsn_t *d = (dcsn_t *)malloc( sizeof( dcsn_t ) );
  17. //d->common_elem = NULL;
  18. d->next_dcsn = NULL;
  19. d->length = 0;
  20. return d;
  21. }
  22.  
  23. void
  24. traverse_dcsns( dcsn_t *d )
  25. {
  26. if( d == NULL ) return;
  27. if( d->common_elem )
  28. {
  29. printf("%c", d->common_elem);
  30. }
  31. traverse_dcsns( d->next_dcsn );
  32. }
  33.  
  34. #define STR_MAX 7
  35. dcsn_t *L[STR_MAX+1][STR_MAX+1];
  36.  
  37. dcsn_t*
  38. find_lcs( char *str1, char *str2, int i, int j )
  39. {
  40. printf("Operating on %u %u\n", i, j);
  41. if( i == 0 || j == 0 )
  42. {
  43. return NULL;
  44. }
  45.  
  46. //if( L[i][j] != NULL ) return L[i][j];
  47.  
  48. dcsn_t *_d;
  49. if( str1[ i-1 ] == str2[ j-1 ] )
  50. {
  51. _d = make_dcsn();
  52. _d->common_elem = str1[ i-1 ];
  53. _d->next_dcsn = find_lcs( str1, str2, i-1, j-1 );
  54. _d->length = 1 + _d->next_dcsn->length;
  55. }
  56. else
  57. {
  58. dcsn_t* d_a = find_lcs( str1, str2, i-1, j );
  59. dcsn_t* d_b = find_lcs( str1, str2, i, j-1 );
  60. if( d_a == NULL || d_b == NULL )
  61. {
  62. if( d_a == NULL && d_b == NULL )
  63. {
  64. return NULL;
  65. }
  66. else if( d_a == NULL )
  67. {
  68. _d = d_b;
  69. }
  70. else
  71. {
  72. _d = d_a;
  73. }
  74. }
  75. else
  76. {
  77. //printf("%u vs %u\n", d_a->length, d_b->length);
  78. if( d_a->length < d_b->length )
  79. {
  80. _d = d_b;
  81. }
  82. else
  83. {
  84. _d = d_a;
  85. }
  86. }
  87. }
  88.  
  89. //L[i][j] = _d;
  90. return _d;
  91. }
  92.  
  93. int main() {
  94. char *str1 = "tester";
  95. char *str2 = "testery";
  96.  
  97. // Initialize memoization
  98. /*
  99. int k, l;
  100. for( k=0; k<STR_MAX; k++ )
  101. {
  102. for( l=0; l<STR_MAX; l++ )
  103. {
  104. L[k][l] = NULL;
  105. }
  106. }
  107. */
  108. printf("hi\n");
  109. // Compute solution
  110. dcsn_t *answer_chain;// = make_dcsn();
  111. //answer_chain = find_lcs( str1, str2, strlen(str1), strlen(str2) );
  112.  
  113. // Output results
  114. //printf("Longest common subsequence is %u bytes long:\t", answer_chain->length);
  115. //traverse_dcsns( answer_chain );
  116. //printf("\n");
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement