/**
* <description> Exact String Matching Algorithm KMP
* calculate the next array of pattern string
* <version> 1.0.1
* <date> August 4, 2004
* <author> Arter
* <email> ArterONE@hotmail.com
* <license> This file is free software; you can redistribute it and/or modify it under the GPL.
* copyright by Arter, 1998 - 2004
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
const int MAX_STRING_LENGTH = 999;
int *getNextArray(const char pattern[]);
int *getNextValArray(const char pattern[]);
int wait();
int main(int argc, char *argv[]) {
char p[MAX_STRING_LENGTH];
int i, m, *next, *nextVal;
if (argc <= 1) {
printf("Usage: next <pattern>/n");
printf("<example created by Arter>/n");
printf("next abcaaaabca");
strcpy(p, "abcaaaabca");
}
else {
if (strlen(argv[1]) > MAX_STRING_LENGTH) argv[1][MAX_STRING_LENGTH] = '/0';
strcpy(p, argv[1]);
}
next = getNextArray(p);
nextVal = getNextValArray(p);
m = strlen(p);
printf("/n[ index ] : ");
for (i = 0; i < m; i ++) printf("%4d", i + 1);
printf("/n[pattern] : ");
for (i = 0; i < m; i ++) printf("%4c", p[i]);
printf("/n[ next ] : ");
for (i = 0; i < m; i ++) printf("%4d", next[i] + 1);
free(next);
printf("/n[nextVal] : ");
for (i = 0; i < m; i ++) printf("%4d", nextVal[i] + 1);
free(nextVal);
printf("/n");
if (argc <= 1) wait();
return 0;
}
int *getNextArray(const char pattern[]) {
int i, j;
int firstIndexOfPattern, lengthOfPattern;
int *next;
lengthOfPattern = strlen(pattern);
if (lengthOfPattern <= 0) {
printf("Error : pattern is null!");
exit(0);
}
else {
next = (int *)malloc(sizeof(int) * lengthOfPattern);
firstIndexOfPattern = 0; //The first index of array is 0 in the C language!
}
//initialized next[0] = -1
next[firstIndexOfPattern] = firstIndexOfPattern - 1;
i = next[firstIndexOfPattern];
j = firstIndexOfPattern + 1;
while (j < firstIndexOfPattern + lengthOfPattern) {
while ((i >= firstIndexOfPattern) && (pattern[i] != pattern[j-1])) {
i = next[i];
}
i ++;
/* the difference between "next" and "nextVal"
if ((i <= lastIndexOfPattern) && (pattern[i] == pattern[j])) {
next[j] = next[i];
}
else {*/
next[j] = i;
//}
j ++;
}
return next;
}
int *getNextValArray(const char pattern[]) {
int i, j;
int firstIndexOfPattern, lengthOfPattern;
int *nextVal;
lengthOfPattern = strlen(pattern);
if (lengthOfPattern <= 0) {
printf("Error : pattern is null!");
exit(0);
}
else {
nextVal = (int *)malloc(sizeof(int) * lengthOfPattern);
firstIndexOfPattern = 0; //The first index of array is 0 in the C language!
}
//initialized next[0] = -1
nextVal[firstIndexOfPattern] = firstIndexOfPattern - 1;
i = nextVal[firstIndexOfPattern];
j = firstIndexOfPattern + 1;
while (j < firstIndexOfPattern + lengthOfPattern) {
while ((i >= firstIndexOfPattern) && (pattern[i] != pattern[j-1])) {
i = nextVal[i];
}
i ++;
if (pattern[i] == pattern[j]) {
nextVal[j] = nextVal[i];
}
else {
nextVal[j] = i;
}
j ++;
}
return nextVal;
}
int wait() {
printf("/n<press any key to exit!>");
getchar();
return 1;
}
计算精确串匹配算法KMP中的next和nextVal数组值
最新推荐文章于 2021-08-27 20:28:40 发布