计算精确串匹配算法KMP中的next和nextVal数组值

/**
 * <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;
}
    
    
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值