Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6KB

  1. #include <stdio.h> /* printf */
  2. #include "utstring.h"
  3. int main()
  4. {
  5. UT_string *s,*t;
  6. char V_TestStr[] = "There are two needle\0s in this \0haystack with needle\0s.";
  7. char V_NeedleStr[] = "needle\0s";
  8. long *V_KMP_Table;
  9. long V_FindPos;
  10. size_t V_StartPos = 0;
  11. size_t V_FindCnt = 0;
  12. utstring_new(s);
  13. utstring_new(t);
  14. utstring_bincpy(s, V_TestStr, sizeof(V_TestStr)-1);
  15. printf("\"%s\" len=%u\n", utstring_body(s), (unsigned)utstring_len(s));
  16. utstring_bincpy(t, V_NeedleStr, sizeof(V_NeedleStr)-1);
  17. printf("\"%s\" len=%u\n", utstring_body(t), (unsigned)utstring_len(t));
  18. V_KMP_Table = (long *)malloc(sizeof(long) * (utstring_len(t) + 1));
  19. if (V_KMP_Table != NULL) {
  20. _utstring_BuildTable(utstring_body(t), utstring_len(t), V_KMP_Table);
  21. do {
  22. V_FindPos = _utstring_find(utstring_body(s) + V_StartPos,
  23. utstring_len(s) - V_StartPos,
  24. utstring_body(t),
  25. utstring_len(t),
  26. V_KMP_Table);
  27. if (V_FindPos >= 0) {
  28. V_FindPos += V_StartPos;
  29. V_FindCnt++;
  30. V_StartPos = V_FindPos + 1;
  31. }
  32. printf("utstring_find()=%ld\n", V_FindPos);
  33. } while (V_FindPos >= 0);
  34. printf("FindCnt=%u\n", (unsigned)V_FindCnt);
  35. free(V_KMP_Table);
  36. } else {
  37. printf("malloc() failed...\n");
  38. }
  39. utstring_free(s);
  40. utstring_free(t);
  41. return 0;
  42. }