字符串匹配
来源:互联网 发布:周杰伦侯佩岑 知乎 编辑:程序博客网 时间:2024/05/29 17:50
/* 字符串匹配:> BM->KMP*/int func(char*s, char*p){ int i = 0; int j = 0; while (i < strlen(s)) { j = 0; while (s[i] == p[j] && j < strlen(p)) { j++; i++; } if (j == strlen(p)) return i - j; i = i - j + 1; } return -1;}void getNext(char*p, int *next){ next[0] = -1; int slow = -1; int fast = 0; while (fast < strlen(p) - 1) { if (slow == -1 || p[slow] == p[fast]) { slow++; fast++; next[fast] = slow; } else slow = next[slow]; }}int KMP(char*s, char*p){ int next[100]; int i = 0; int j = 0; getNext(p, next); while (i < strlen(s)) { if (j == -1 || s[i] == p[j]) { ++i; ++j; } else { j = next[j]; } if (j == strlen(p)) return i - strlen(p); } return -1;}
关于next数组和KMP参见链接:
http://note.youdao.com/share/?id=f9906cb0d53a384435d83d66b59fafa5&type=note
http://note.youdao.com/share/?id=2312592fd636b64f270d18240dbb0268&type=note
2 0
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配...
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配
- 字符串匹配
- restoredefaultpath matlab
- hdu 1358 Period(求循环节)
- python自学总结
- 关于android5.0一些主题颜色的介绍
- 设计模式之观察者模式
- 字符串匹配
- Android MeasuerSpec的由来及使用
- 商业级项目登录模块源码分析
- ReactJS组件生命周期详述
- Android之图片缓存
- 【Java集合源码剖析】Vector源码剖析
- 【poj 2892】Tunnel Warfare 题意&题解&代码(C++)
- winbox常用功能
- C语言(彻底理解指针和地址,彻底!)