KMP算法源码
来源:互联网 发布:淘宝店卖保健品 编辑:程序博客网 时间:2024/05/18 18:01
#include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 void getNextVal(char* T, int* nextVal);
6 int IndexKMP(char* S, char* T);
7 void getNextVal(char* T, int* nextVal)
8 {
9 int i = 1, j = 0;
10 int nLen = strlen(T);
11 printf("getNextVal-----nLen=%d\n", nLen);
12 nextVal[0] = 0;
13 while (i < nLen - 1)
14 {
15 if (T[i] == T[j])
16 {
17 ++i;
18 ++j;
19 if (T[i] != T[j])
20 {
21 nextVal[i] = j;
22 }
23 else
24 {
25 nextVal[i] = nextVal[j];
26 }
27 }
28 else if (j == 0)
29 {
30 ++i;
31 nextVal[i] = j;
32 }
33 else
34 {
35 j = nextVal[j];
36 }
37 }
38 }
39
40 int IndexKMP(char* S, char* T)
41 {
42 int nLenT = strlen(T);
43 int nLenS = strlen(S);
44 int i = 0,j = 0;
45 int* nextVal = (int*)malloc(sizeof(int) * nLenT);
46 printf("IndexKMP----------nLenT=%d\n", nLenT);
47 printf("IndexKMP----------nLenS=%d\n", nLenS);
48 getNextVal(T, nextVal);
49
50 while (i <= nLenS && j < nLenT)
51 {
52 if (S[i] == T[j])
53 {
54 ++i;
55 ++j;
56 }
57 else if (j == 0)
58 {
59 ++i;
60 }
61 else
62 {
63 j = nextVal[j];
64 }
65 }
66
67 if (j >= nLenT)
68 {
69 return i - nLenT;
70 }
71 else
72 {
73 return 0;
74 }
75 }
76
77 int main(int argc, char* argv[])
78 {
79 char* S = "ababcabcacbab";
80 char* T = "abcac";
81 printf("position = %d\n",IndexKMP(S,T));
82 return 0;
83 }
84
85
2 #include <stdlib.h>
3 #include <string.h>
4
5 void getNextVal(char* T, int* nextVal);
6 int IndexKMP(char* S, char* T);
7 void getNextVal(char* T, int* nextVal)
8 {
9 int i = 1, j = 0;
10 int nLen = strlen(T);
11 printf("getNextVal-----nLen=%d\n", nLen);
12 nextVal[0] = 0;
13 while (i < nLen - 1)
14 {
15 if (T[i] == T[j])
16 {
17 ++i;
18 ++j;
19 if (T[i] != T[j])
20 {
21 nextVal[i] = j;
22 }
23 else
24 {
25 nextVal[i] = nextVal[j];
26 }
27 }
28 else if (j == 0)
29 {
30 ++i;
31 nextVal[i] = j;
32 }
33 else
34 {
35 j = nextVal[j];
36 }
37 }
38 }
39
40 int IndexKMP(char* S, char* T)
41 {
42 int nLenT = strlen(T);
43 int nLenS = strlen(S);
44 int i = 0,j = 0;
45 int* nextVal = (int*)malloc(sizeof(int) * nLenT);
46 printf("IndexKMP----------nLenT=%d\n", nLenT);
47 printf("IndexKMP----------nLenS=%d\n", nLenS);
48 getNextVal(T, nextVal);
49
50 while (i <= nLenS && j < nLenT)
51 {
52 if (S[i] == T[j])
53 {
54 ++i;
55 ++j;
56 }
57 else if (j == 0)
58 {
59 ++i;
60 }
61 else
62 {
63 j = nextVal[j];
64 }
65 }
66
67 if (j >= nLenT)
68 {
69 return i - nLenT;
70 }
71 else
72 {
73 return 0;
74 }
75 }
76
77 int main(int argc, char* argv[])
78 {
79 char* S = "ababcabcacbab";
80 char* T = "abcac";
81 printf("position = %d\n",IndexKMP(S,T));
82 return 0;
83 }
84
85
- KMP算法源码
- KMP算法源码备份
- KMP匹配算法源码
- kmp算法C++源码实现
- 浅析KMP算法(附C++源码)
- [转]KMP算法实现(源码c/c++)
- KMP及其扩展算法——源码、注释
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- Objective-C属性介绍
- ASCII码
- DynamicQuery一张或者多张表
- 什么是声明?什么是定义?
- iphone ios NSLog To File 到文件
- KMP算法源码
- 《第十六周实验报告任务1——涨工资并排序 》
- database的l主要对象
- VS.NET开发环境操作小技巧
- C++ 类,运算符重载--->代码
- 改善用户体现小体现_文字不可选择
- 解决vim乱码
- 伙伴内存系统之 zone_water_mark_ok
- 内核中调用spi接口