My understand of KMP describe in C
来源:互联网 发布:知彼网络 编辑:程序博客网 时间:2024/05/23 12:55
FIRST:KMP--abstract
It's a algorithm improving from sample matching algorithm (BF algorithm).
It's detail description can view in WIKI from below:
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
SECOND:the coding in Cpp
below code is from github!!
the kmp.h file
the test demo kmp_demo.cpp
It's a algorithm improving from sample matching algorithm (BF algorithm).
It's detail description can view in WIKI from below:
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
SECOND:the coding in Cpp
below code is from github!!
the kmp.h file
点击(此处)折叠或打开
- /*******************************************************************************
- * ALGORITHM IMPLEMENTAIONS
- *
- * /\ | _ _ ._ o _|_ |_ ._ _ _
- * /--\ | (_| (_) | | |_ | | | | | _>
- * _|
- *
- * KNUTH-MORRIS-PRATT ALGORITHMS
- *
- * Features:
- * Complexity is O(n + k), where n is the target string length,
- * and k is the pattern length
- *
- * http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
- *
- ******************************************************************************/
- #ifndef __KMP_H__
- #define __KMP_H__
- #include <string.h>
- namespace alg {
- static void kmp_table(const char *W, int * T, int len);
- /**
- * S -> the text to be searched
- * W -> the word to search
- * return the position where W is found S
- */
- static int kmp_search(const char * S, const char * W) {
- int LEN_S = strlen(S);
- int LEN_W = strlen(W);
- int m = 0;
- int i = 0;
- int T[LEN_W];
- kmp_table(W,T, LEN_W);
- while (m+i < LEN_S) {
- if (W[i] == S[m+i]) {
- if (i == LEN_W -1) {
- return m;
- }
- i++;
- } else {
- m = m+i-T[i];
- if (T[i] > -1) {
- i = T[i];
- } else {
- i = 0;
- }
- }
- }
- return -1;
- }
- /**
- * build a table for the word to be searched
- * eg:
- * i 0 1 2 3 4 5 6
- * W[i] A B C D A B D
- * T[i] -1 0 0 0 0 1 2
- */
- static void kmp_table(const char *W, int * T, int len) {
- int pos = 2; // the current position we are computing in T
- int cnd = 0; // the next character of the current candidate substring
- T[0] = -1;
- T[1] = 0;
- while (pos < len) {
- // first case: the substring continues
- if (W[pos-1] == W[cnd]) {
- cnd++;
- T[pos] = cnd;
- pos++;
- } else if (cnd >0) { // second case: it doesn't, but we can fall back
- cnd = T[cnd];
- } else { // third case: we have run out of candidates. Note cnd = 0
- T[pos] = 0;
- pos++;
- }
- }
- }
- }
- #endif //
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include "kmp.h"
- using namespace alg;
- int main(void)
- {
- srand(time(NULL));
- char * S = (char*)malloc(1001);
- char * W = (char*)malloc(5);
- memset(S,0, 1001);
- memset(W,0, 5);
- // random genrate a pattern for A, G, C,T
- const char P[] = {'A', 'G','C','T'};
- for (int i=0;i<1000;i++) {
- int k = rand()%4;
- S[i] = P[k];
- }
- for (int i=0;i<4;i++) {
- int k = rand()%4;
- W[i] = P[k];
- }
- // do a search for W from S
- int pos = kmp_search(S, W);
- printf("to be searched:%s\n", W);
- if (pos > 0) {
- printf("found in pos:%d\n", pos);
- printf("text:\n%.*s", pos, S);
- printf("\033[31m%s\033[0m", W);
- printf("%s\n",&S[pos + strlen(W)]);
- }
- }
0
上一篇:My understand of stack and queue
下一篇:My first CXX problem:"aggregate has incomplete type and cannot be defined"
相关热门文章
- test123
- 编写安全代码——小心有符号数...
- 使用openssl api进行加密解密...
- 一段自己打印自己的c程序...
- sql relay的c++接口
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
0 0
- My understand of KMP describe in C
- My understand of "iptables"
- My understand of Euclidean distance in digital image processing
- My understand of stack and queue
- deeply understand pointer in C
- Question 4: Which of the following statements correctly describe the code below in C++?
- Question 28: Which of the following statements accurately describe the new[] operator in C++?
- Question 9: Which of the following statements correctly describe the results of executing the code below in C++?
- Question 15: Which of the following statements describe the results of executing the code snippet below in C++?
- first step in order to optimization of my C program
- Question 11: Which of the following statements describe the result when standard new cannot allocate the requested storage in C+
- Question 27: Which of the following reasons describe why a destructor cannot throw an exception in C++?
- Question 48: In C++, which of the following statements accurately describe a base class destructor calling a virtual function ov
- Understand the Impact of Low-Lock Techniques in Multithreaded Apps
- Describe technolegy in English
- Effective Objective-C: Item11 Understand Role of obj_msgSend笔记
- Do you understand my mind...
- My first c in Ubuntu
- hdu3377plan【哈密顿路径插头Dp】
- build makefile for my coding!!
- My understand of "iptables"
- shell 编程语法总结(I/O 逻辑控制结构)
- My understand of stack and queue
- My understand of KMP describe in C
- My first CXX problem:"aggregate has incomplete type and cannot be defined"
- define function pointer
- notes of C++
- My start of git
- 对搜索引擎的初了解发展及核心框架
- 搜索引擎之网络爬虫
- my makefile for CPP v2.0
- 常见ddl无刷联动效果
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
深蓝色上衣
男士深蓝色衬衫配什么裤子
深蓝色羊绒大衣的搭配
深蓝色的七千米读后感
深蓝色衬衫搭配
深蓝色毛衣配什么外套
深蓝色胸罩
深蓝色大衣配什么颜色围巾
深蓝色针织衫搭配
深蓝色墙纸
深蓝色英文
深蓝色英语怎么说
蓝色配什么色好看
深蓝色的英文
深蓝色皮沙发
深蓝色板鞋
深蓝色双肩包
深蓝色休闲裤
深蓝色装饰公司
深蓝色西服
深蓝色休闲西装搭配
深蓝色家装
深蓝色布鞋
深蓝色外套
深蓝色背景
深蓝色裙子配什么鞋子
深蓝色指甲油
深蓝色运动衣
深蓝色钱包好不好
深蓝色装修
深蓝色t恤配什么裤子
深蓝色裤子配什么颜色鞋子
深蓝色鞋子
深蓝色眼线
深蓝色的眼泪
深蓝色配什么颜色
深蓝色包包
深蓝色针织开衫
深蓝色配黑色
皮肤黑 深蓝色
深蓝色外套怎么搭配