小算法--KMP
2013-03-14 22:57
363 查看
#include <stdio.h> #include <stdlib.h> #define MAXSTRLEN 255 typedef unsigned char String[MAXSTRLEN + 1]; int main(void) { int Index_KMP(String S, String T, int next[], int pos);//利用模式串T的next求T在主串S中第pos个字符之后的位置的KMP算法 void get_next(String, int[]);//求模式串T的next值 int next[MAXSTRLEN + 1] = {0}; String S=" acabaabaabcacaabc";//主串 String T=" abaabcac";//模式串 S[0] = 17; T[0] = 8; get_next(T, next); printf("%d\n", Index_KMP(S, T, next, 1)); return EXIT_SUCCESS; } int Index_KMP(String S, String T, int next[], int pos) { int i = pos, j = 1; while (i <= S[0] && j <= T[0]) { if (0 == j || S[i] == T[j]) { i++;j++; } else { j = next[j]; } } if (j > T[0]) { return i - T[0]; } else { return 0; } } void get_next(String T, int next[]) { int i = 1, j = 0; next[1] = 0; while (i < T[0]) { if (0 == j || T[i] == T[j]) { i++;j++; if (T[i] != T[j]) { next[i] = j; } else { next[i] = next[j]; } } else { j = next[j]; } } }
相关文章推荐
- 【无浪】串之KMP算法之二
- KMP字符串模式匹配算法
- 看毛片算法 KMP
- kmp 算法
- BF,KMP,BM三种字符串匹配算法性能比较
- 【经典算法】——KMP,深入讲解next数组的求解 【待研究】
- 算法入门之KMP
- kmp经典算法
- KMP(改进模式匹配算法)--串
- KMP文本匹配算法
- Boyer-Moore文本匹配算法(联合使用KMP和Horspool算法)
- KMP模式匹配算法中next,nextval的分别实现
- 字符串查找算法总结(暴力匹配、KMP 算法、Boyer-Moore 算法和 Sunday 算法)
- KMP常用算法模板
- 模式匹配算法KMP详解
- 第五章 KMP匹配算法
- 【经典算法】——KMP,深入讲解next数组的求解
- 数据结构——串的朴素模式和KMP匹配算法
- 基础算法 扩展KMP
- KMP经典算法解析