KMP算法 (c++ 构造完整的DFA)
来源:互联网 发布:网易邮箱大师mac版 编辑:程序博客网 时间:2024/06/05 18:01
vi kmp.h
#pragma once#include <string>class KMP{private: int **dfa; std::string pattern; int R;private: void init_DFA(const std::string& p) { dfa[p[0]][0] = 1; for (int j = 1, state = 0; j < p.length(); ++j) { for (int i = 0; i < R; ++i) { dfa[i][j] = dfa[i][state]; } dfa[p[j]][j] = j + 1; state = dfa[p[j]][state]; } }public: KMP(const std::string& p, const int &R = 256):dfa(new int*[R]),pattern(p),R(R) { for (int i = 0; i < R; ++i) dfa[i] = new int[p.length()]; for (int i = 0; i < R; ++i) for (int j = 0; j < p.length(); ++j) dfa[i][j] = 0; init_DFA(p); } int indexOf(const std::string& s) { int i, j; for (i = 0, j = 0; i < s.length() && j < pattern.length(); ++i) { j = dfa[s[i]][j]; } if (j == pattern.length()) return i - pattern.length(); else return s.length(); }};
main.cpp
#include <iostream>#include "KMP.h"using namespace std;int main(){ KMP kmp("AACAA"); cout << kmp.indexOf("AABRAACADABRAACAADABRA") << endl; system("pause"); return 0;}
阅读全文
0 0
- KMP算法 (c++ 构造完整的DFA)
- KMP算法中关于构造DFA部分的纠结
- (一)KMP算法的DFA解法
- 基于dfa的kmp算法思想
- KMP算法实现字符串的模式匹配完整C代码
- 编译原理中正则表达式直接构造DFA,DFA的最小化算法
- 编译原理中正则表达式直接构造DFA,DFA的最小化算法
- 从DFA角度理解KMP算法
- KMP算法DFA方式实现解析
- 完整的KMP算法(包括求出next)
- 查找-基于DFA的KMP字符串匹配
- KMP算法的具體實現 (C++)
- kmp和dfa
- KMP(DFA)
- 【★】KMP算法完整教程
- DFA算法的实现与最小化
- [C++] [算法] KMP算法
- 基于DFA的C程序注释删除
- Leetcode 107
- 用C语言实现的学生管理系统
- 插入排序
- ROS消息的创建
- course 系统1.2
- KMP算法 (c++ 构造完整的DFA)
- 递归
- Exchange Seats
- 勾股定理及其逆定理的一种简单证明方法
- 线程、进程和通信
- leetcode之二叉树类之最小公共祖先-----236/235. Lowest Common Ancestor of a Binary/Binary Search Tree
- mybatis学习三之mapper.xml开发方法
- Bode Plot
- poj算法-使用C语言在VC++6.0实现poj1753(递归枚举)