Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define MIN(a,b) (((a)<(b))?(a):(b))
  4.  
  5. struct dictionary{
  6. int value;
  7. int count;
  8. struct dictionary* next;
  9.  
  10. };
  11. typedef struct dictionary item;
  12.  
  13.  
  14. item* hash(item* head,int index){
  15. item* iterator = head;
  16. while(iterator->next){
  17. if(iterator->value == index){
  18. return iterator;
  19. }
  20. iterator = iterator->next;
  21. }
  22. return NULL;
  23. }
  24.  
  25. item* push(int v,int c,item* curr){
  26. curr->value = v;
  27. curr->count = c;
  28. curr->next = (item*)malloc(sizeof(item));
  29. return curr->next;
  30. }
  31.  
  32.  
  33.  
  34. static int scores(int src[],int tgt[],int ax,int ay){
  35. int i,j,k;
  36. int scores[ax+1][ay+1];
  37. item *head,*curr,*temp;
  38. head = (item*)malloc(sizeof(item));
  39. curr = head;
  40.  
  41. int INF = ax + ay;
  42. scores[0][0] = INF;
  43.  
  44. /* handle blank strings */
  45. if(ax == 0){
  46. return ay;
  47. }else if(ay == 0){
  48. return ax;
  49. }
  50.  
  51. /* setup scoring matrix */
  52. for(i=0;i<=ax;i++){
  53. scores[i+1][1] = i;
  54. scores[i+1][0] = INF;
  55.  
  56. if(hash(head,src[i]) == NULL){
  57. curr = push(src[i],0,curr);
  58. }
  59. }
  60. for(j=0;j<=ay;j++){
  61. scores[1][j+1] = j;
  62. scores[0][j+1] = INF;
  63.  
  64. if(hash(head,tgt[j]) == NULL){
  65. curr = push(tgt[j],0,curr);
  66. }
  67. }
  68.  
  69.  
  70. /* work loop */
  71. int db,i1,j1;
  72. for(i=1;i<=ax;i++){
  73. db = 0;
  74. for(j=1;j<=ay;j++){
  75. i1 = hash(head,tgt[j-1])->count;
  76. j1 = db;
  77.  
  78. if(src[i-1] == tgt[j-1]){
  79. scores[i+1][j+1] = scores[i][j];
  80. db = j;
  81. }else{
  82. scores[i+1][j+1] = MIN(scores[i][j], MIN(scores[i+1][j], scores[i][j+1])) + 1;
  83. }
  84.  
  85. scores[i+1][j+1] = MIN(scores[i+1][j+1], scores[i1][j1] + i - i1 - 1 + j - j1);
  86. }
  87.  
  88. hash(head,src[i-1])->count = i;
  89. }
  90.  
  91. return scores[ax+1][ay+1];
  92. }
  93.  
  94.  
  95.  
  96. int main(void) {
  97. // Same code as cxs_distance
  98. // Left for ease of compiling outside of perl
  99.  
  100. int arrSourcex[4];
  101. int arrTargetx[4];
  102.  
  103. arrSourcex[0] = 1;
  104. arrSourcex[1] = 2;
  105. arrSourcex[2] = 3;
  106. arrSourcex[3] = 4;
  107.  
  108. arrTargetx[0] = 1;
  109. arrTargetx[1] = 3;
  110. arrTargetx[2] = 2;
  111. arrTargetx[3] = 4;
  112.  
  113.  
  114. int lenSourcex2 = 4;
  115. int lenTargetx2 = 4;
  116.  
  117. int x = scores(arrSourcex,arrTargetx,lenSourcex2,lenTargetx2);
  118. printf("%i\n",x);
  119.  
  120. return x;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement