C++实现KMP算法(C风格)
2015-10-07 17:05
489 查看
/* * Main.cpp * * Created on: Oct 7, 2015 * Author: chris */ #include<iostream> #include<cstring> using namespace std; bool get_next(const char* pat, int*& next) { int len = strlen(pat); next = new int[len]; if(!next) return false; next[0] = 0; int i = 1, j = 0; while(i < len) { if(pat[i] == pat[j]) { // matched. ++i; ++j; // move on. next[i] = j; // record next. } else if (j != 0){ // doesn't matched. j = next[j]; // back trace. } else { // nomatched. next[i] = 0; ++i; // skip. } }//while return true; } enum { INDS_INIT_SIZE = 10, INDS_INC_SIZE = 10}; bool Index_KMP(const char* str, const char *pat, int pos, int*& inds) { int *next = NULL; if(!get_next(pat, next)) // prepare. return false; inds = new int[INDS_INIT_SIZE]; if(!inds) { delete next; return false; } inds[0] = INDS_INIT_SIZE; // arr size inds[1] = 0; // num of matched ind int i = pos, j = 0; int Slen = strlen(str), Plen = strlen(pat); while(i < Slen) { if(str[i] == pat[j]) { // matched. ++i; ++j; // move on. if(j >= Plen) { // found. int ind = i - Plen; //record if(inds[1] + 2 >= inds[0]) { int*temp = new int[inds[0] + INDS_INC_SIZE]; if(!temp) { delete inds; delete next; return false; } for(int i = 0; i < inds[0]; ++i) temp[i] = inds[i]; delete inds; inds = temp; inds[0] += INDS_INC_SIZE; }//realloc inds[inds[1] + 2] = ind; ++inds[1]; j = 0; // continue. } } else if(j != 0) { // back trace. j = next[j]; } else { // impossible. ++i; } }//while delete next; return true; } int main(void) { string str1, str2; while(cin >> str1 >> str2) { int* found = NULL; if(Index_KMP(str1.c_str(), str2.c_str(), 0, found)){ int * data = found + 2; for(int i = 0; i < found[1]; ++i) cout << data[i] << " "; cout << endl; delete found; } else cout << "failed." << endl; } return 0; }
相关文章推荐
- C语言:计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
- 求出0~999之间的所有“水仙花数”并输出
- 输出 和*组成的菱形
- 编写程序数一下 1到 100 的所有整数中出现多少次数字 9
- 输出一个整数的每一位
- c++STL之vector
- 计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
- 输出一个整数的每一位
- cryengine3 C++添加结点,制作插件
- 关于C语言中的typedef
- 订阅发布模式——C++实现
- c++STL之string
- C++实现串的最小操作子集
- 面试复习(C++)之基数排序
- C++引用(References)
- C++学习笔记1 - 预备知识
- C语言基础学习——第10天(文件操作)
- C++学习小结之注意细节点
- typedef struct 和 struct
- C++函数