Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void computeLPSArray(char *pat, int M, int *lps) {
- int len = 0;
- lps[0] = 0;
- int i = 1;
- while (i < M) {
- if (pat[i] == pat[len]) {
- len++;
- lps[i] = len;
- i++;
- } else {
- if (len != 0) {
- len = lps[len - 1];
- } else {
- lps[i] = 0;
- i++;
- }
- }
- }
- }
- void kmpWildcardSearch(char *pat, char *txt) {
- int M = strlen(pat);
- int N = strlen(txt);
- int *lps = (int *)malloc(sizeof(int) * M);
- computeLPSArray(pat, M, lps);
- int i = 0;
- int j = 0;
- while (i < N) {
- if (pat[j] == txt[i] || pat[j] == '*') {
- j++;
- i++;
- }
- if (j == M) {
- printf("Pattern found at index %d\n", i - j);
- j = lps[j - 1];
- } else if (i < N && (pat[j] != txt[i] && pat[j] != '*')) {
- if (j != 0)
- j = lps[j - 1];
- else
- i++;
- }
- }
- free(lps);
- }
- int main() {
- char txt[] = "afjkarbdsabacba";
- char pat[] = "a*b";
- kmpWildcardSearch(pat, txt);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement