Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void FillTheTable(char *str, int len, int chars[256])
- {
- int i;
- for (i = 0; i < 256; i++)
- chars[i] = -1;
- for (i = 0; i < len; i++)
- chars[(int)str[i]] = i;
- }
- int search(char *text, char *example)
- {
- int exampleLen = strlen(example);
- int textLen = strlen(text);
- int charTable[256];
- FillTheTable(example, exampleLen, charTable);
- int shift = 0;
- while (shift <= (textLen - exampleLen))
- {
- int j = exampleLen - 1;
- while (j >= 0 && example[j] == text[shift + j])
- j--;
- if (j < 0)
- return shift;
- else
- if (j - charTable[text[shift + j]] > 0)
- shift += j - charTable[text[shift + j]]; //Сдвигаемся на ближайший символ в образце, равный текущему
- else
- shift += j + 1; //Иначе сдвигаемся на j позиций
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement