KMP算法
来源:互联网 发布:阿里云 共享存储 编辑:程序博客网 时间:2024/06/06 17:45
#include <stdio.h>#include <string.h>int next[32] = {-999};/* 返回模式串T在母串S中第pos个字符的位置 *//* 调试小技巧 print x = value 或 set var x = value 可以改变gdb运行时变量的值 */int index_BM(char *S, char *T, int pos){ int i; int j; i = pos; j = 0; while ( (i < strlen(S)) && (j < strlen(T)) ) { if (S[i] == T[j]) { i++; j++; } else { i = i - j + 1; j = 0; } } /* 注意strlen(T)意味着j的取值范围为0 ~ (strlen(T) - 1) */ if (strlen(T) == j) { return i - strlen(T); } else { return -1; }}void get_next(char *T, int *next){ int k = -1; int j = 0; next[j] = k; while (j < strlen(T)) { if ( (k == -1) || (T[j] == T[k]) ) //注意等号是==,而不是= { ++k; // 注意是先加后使用 ++j; next[j] = k; } else { k = next[k]; } }}int index_KMP(char *S, char *T, int pos){ int i; int j; i = pos; j = 0; while ( (i < strlen(S)) && (j < strlen(T)) ) { /* j = -1 表示next[0], 说明失配处在模式串T的第0个字符。所以这里特殊处理,然后令i+1和j+1。*/ if ( (j == -1) || S[i] == T[j]) { i++; j++; } else { j = next[j]; } } if (strlen(T) == j) { return i - strlen(T); } else { return -1; }}void print_next(int next[], int n){ int i; for (i = 0; i < n; i++) { printf("next[%d] = %d\n", i, next[i]); }}int main(){ char s[30] = "ababcabcacbab"; char t[30] = "abgabm"; int pos = 0; int index; printf("================ BM ==============\n"); index = index_BM(s, t, pos); printf("index = %d\n", index); printf("================ KMP ==============\n"); get_next(t, next); print_next(next, strlen(t)); index = index_KMP(s, t, pos); printf("index = %d\n", index); return 0;}
0 0
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- KMP算法
- 高效工作
- VS使用protobuf和gflag的一体化解决方案
- Topic On ARM Core
- float转换为二进制
- 实习生刚入职 两周总结
- KMP算法
- Volley的使用
- ScrollView如何正常响应自己添加的手势
- Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去其他地找了)---zhangkaitao
- 【设计配色宝典】设计师必备七色配色宝典,附AI源文件!
- java nio aio bio
- Andriod的活动生存期
- git入门
- jQuery 的.serialize()