-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.json
1 lines (1 loc) · 2.79 KB
/
content.json
1
[{"title":"kmp","date":"2017-08-08T17:50:56.000Z","path":"2017/08/09/kmp/","text":"KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt同时发现,因此人们称它为克努特——莫里斯——普拉特操作(简称KMP算法)。KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的。具体实现就是实现一个next()函数,函数本身包含了模式串的局部匹配信息。时间复杂度O(m+n)。1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495//// kmp.c// //// Created by LiveMac on 2017/8/8.////#include <stdio.h>#include <string.h>void get_next(char* T, int* next){ int i = 1; int j = 0; next[1] = 0; while (i < T[0]) { if (j == 0 || T[i] == T[j]) { i++; j++; if (T[i] != T[j]) { next[i] = j; } else { next[i] = next[j]; } } else { j = next[j]; } }}int index_kmp(char* S, char* T, int pos){ int i = pos; int j = 1; int next[255]; get_next(T, next); while(i <= S[0] && j <= T[0]) { if (j == 0 || S[i] == T[j]) { i++; j++; } else { j = next[j]; } } if (j > T[0]) { return i - T[0]; } else { return 0; }}int main(){ char S[255] = " aasssbbsdfdsfas"; S[0] = strlen(S) - 1; char T[255] = " sssb"; char Input[255]; int pos = 1; printf("check[%s][%d], please input substr:\\n", S, S[0]); while(1) { scanf("%s",Input); int i = 0; while(Input[i] != '\\0') { T[i+1] = Input[i]; i++; } T[i+1] = '\\0'; T[0] = strlen(T) - 1; printf("Input[%s][%lu] -> T[%s][%lu][%d]\\n", Input, strlen(Input), T, strlen(T), T[0]); int ret = index_kmp(S, T, pos); if (ret) { printf("[%s] has substr [%s] in pos [%d]\\n\\n", S, T, ret); } else { printf("[%s] no substr [%s]\\n\\n", S, T); } }}","tags":[{"name":"算法","slug":"算法","permalink":"http://hanchunyu.cn/tags/算法/"},{"name":"字符串","slug":"字符串","permalink":"http://hanchunyu.cn/tags/字符串/"}]},{"title":"Hello World","date":"2017-08-08T17:32:59.000Z","path":"2017/08/09/hello-world/","text":"Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub. Quick StartCreate a new post1$ hexo new \"My New Post\" More info: Writing Run server1$ hexo server More info: Server Generate static files1$ hexo generate More info: Generating Deploy to remote sites1$ hexo deploy More info: Deployment","tags":[]}]