Advertisement
meta1211

Untitled

Mar 10th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. void FillTheTable(char *str, int len, int chars[256])
  2. {
  3. int i;
  4. for (i = 0; i < 256; i++)
  5. chars[i] = -1;
  6. for (i = 0; i < len; i++)
  7. chars[(int)str[i]] = i;
  8. }
  9.  
  10. int search(char *text, char *example)
  11. {
  12. int exampleLen = strlen(example);
  13. int textLen = strlen(text);
  14.  
  15. int charTable[256];
  16. FillTheTable(example, exampleLen, charTable);
  17.  
  18. int shift = 0;
  19. while (shift <= (textLen - exampleLen))
  20. {
  21. int j = exampleLen - 1;
  22. while (j >= 0 && example[j] == text[shift + j])
  23. j--;
  24. if (j < 0)
  25. return shift;
  26. else
  27. if (j - charTable[text[shift + j]] > 0)
  28. shift += j - charTable[text[shift + j]]; //Сдвигаемся на ближайший символ в образце, равный текущему
  29. else
  30. shift += j + 1; //Иначе сдвигаемся на j позиций
  31. }
  32. return -1;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement