Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- static int calc_skip(char *);
- static int faster_match(char *, char *, int);
- #define CHARS 128
- static int skip[CHARS];
- int main(void) {
- char *text = "XYXZde0XZZkWXYZ";
- char *pattern = "WXYZ";
- int n = strlen(text);
- int ret = faster_match(text, pattern, n);
- if (ret < 0) {
- exit(1);
- }
- printf("%d\n", ret);
- printf("%s\n", text + ret);
- }
- static int calc_skip(char *pattern) {
- int i, m;
- m = strlen(pattern);
- for (i = 0; i < CHARS; ++i) {
- skip[i] = m;
- }
- for (i = 0; i < m; ++i) {
- skip[(int) pattern[i]] = m - i - 1;
- }
- return m;
- }
- static int faster_match(char *text, char *pattern, int n) {
- int m = calc_skip(pattern) - 1;
- int i = m, j;
- while (i < n) {
- j = m;
- while (j >= 0) {
- if (text[i] == pattern[j]) {
- if (j == 0) {
- return i;
- }
- --i, --j;
- } else {
- break;
- }
- }
- i += (skip[(int) text[i]] > m - j ? skip[(int) text[i]] : m - j + 1);
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement