fork download
  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. typedef char BIT; /* needs only to hold the values 0 and 1 */
  5.  
  6. const char *bitap_search(const char *text, const char *pattern)
  7. {
  8. const char *result = NULL;
  9. int m = strlen(pattern);
  10. BIT *R;
  11. int i, k;
  12.  
  13. if (pattern[0] == '\0') return text;
  14.  
  15. /* Initialize the bit array R */
  16. R = malloc((m+1) * sizeof *R);
  17. R[0] = 1;
  18. for (k=1; k <= m; ++k)
  19. R[k] = 0;
  20.  
  21. for (i=0; text[i] != '\0'; ++i) {
  22. /* Update the bit array. */
  23. for (k=m; k >= 1; --k)
  24. R[k] = R[k-1] && (text[i] == pattern[k-1]);
  25.  
  26. if (R[m]) {
  27. result = (text+i - m) + 1;
  28. break;
  29. }
  30. }
  31.  
  32. free(R);
  33. return result;
  34. }
  35.  
Runtime error #stdin #stdout 0.03s 10832KB
stdin
Standard input is empty
stdout
Standard output is empty