C++实现Kmp字符匹配算法的优化版
2013-06-25 21:37
585 查看
C++实现Kmp字符匹配算法的优化版
头文件:KmpAlgorithm.h
[cpp] view
plaincopy
#ifndef KMPALGORITHM_H
#define KMPALGORITHM_H
#include <string>
#include <iostream>
class KmpAlgorithm{
std::string s; //主串
std::string t; //子串
static const int MaxSize = 255;
int next[MaxSize];
void Get_Next(); //T串的模式匹配函数
public:
KmpAlgorithm(){
std::memset(next,0,sizeof(int) * 255);
printf("请输入要匹配的字符串的主串:\n");
std::cin >> s;
printf("请输入要匹配的字符串的子串:\n");
std::cin >> t;
}
int Index_Kmp(int pos); //字符匹配函数
};
#endif //KMPALGORITHM_H
实现文件:KmpAlgorithm.cpp
[cpp] view
plaincopy
#include "KmpAlgorithm.h"
void KmpAlgorithm::Get_Next()
{
int i,j;
i = 0;
j = -1;
next[0] = -1;
while(i < t.size()-1)
{
if(j == -1 || t[j] == t[i]) //如果相等则继续
{ //next[0] == -1 j的值有可能回溯为-1数组越界
++i;
++j;
if(t[j] != t[i]) //如果下面两个字符不相等则把j的值赋给next在i位置的值
next[i] = j;
else
next[i] = next[j]; //相等则把next在j位置的值赋给next在i位置的值
}
else
j = next[j];
}
}
int KmpAlgorithm::Index_Kmp(int pos) //字符匹配函数
{
int i = pos - 1; //数组从下标0开始
int j = 0;
Get_Next();
while(i < s.size() && j < t.size())
{
if(j == -1 || s[i] == t[j]) //如果相等继续
{ //如果j的值回溯到-1 next[0] == -1 则继续否则数组越界
++i;
++j;
}
else
{
j = next[j]; //不相等则回溯
}
}
if(j >= t.size()) //匹配成功返回在主串中的位置
return i - t.size() + 1;
else
return -1; //失败返回-1
}
测试文件:main.cpp
[cpp] view
plaincopy
#include "KmpAlgorithm.h"
#include <iostream>
using namespace std;
int main()
{
int pos = 0;
KmpAlgorithm km;
printf("请输入在主串的第几个字符开始匹配:\n");
scanf("%d",&pos);
int position = km.Index_Kmp(pos);
if(position == -1)
printf("在主串中未找到与子串匹配的字符:\n");
else
printf("在主串的第%d个位置匹配成功\n",position);
return 0;
}
头文件:KmpAlgorithm.h
[cpp] view
plaincopy
#ifndef KMPALGORITHM_H
#define KMPALGORITHM_H
#include <string>
#include <iostream>
class KmpAlgorithm{
std::string s; //主串
std::string t; //子串
static const int MaxSize = 255;
int next[MaxSize];
void Get_Next(); //T串的模式匹配函数
public:
KmpAlgorithm(){
std::memset(next,0,sizeof(int) * 255);
printf("请输入要匹配的字符串的主串:\n");
std::cin >> s;
printf("请输入要匹配的字符串的子串:\n");
std::cin >> t;
}
int Index_Kmp(int pos); //字符匹配函数
};
#endif //KMPALGORITHM_H
实现文件:KmpAlgorithm.cpp
[cpp] view
plaincopy
#include "KmpAlgorithm.h"
void KmpAlgorithm::Get_Next()
{
int i,j;
i = 0;
j = -1;
next[0] = -1;
while(i < t.size()-1)
{
if(j == -1 || t[j] == t[i]) //如果相等则继续
{ //next[0] == -1 j的值有可能回溯为-1数组越界
++i;
++j;
if(t[j] != t[i]) //如果下面两个字符不相等则把j的值赋给next在i位置的值
next[i] = j;
else
next[i] = next[j]; //相等则把next在j位置的值赋给next在i位置的值
}
else
j = next[j];
}
}
int KmpAlgorithm::Index_Kmp(int pos) //字符匹配函数
{
int i = pos - 1; //数组从下标0开始
int j = 0;
Get_Next();
while(i < s.size() && j < t.size())
{
if(j == -1 || s[i] == t[j]) //如果相等继续
{ //如果j的值回溯到-1 next[0] == -1 则继续否则数组越界
++i;
++j;
}
else
{
j = next[j]; //不相等则回溯
}
}
if(j >= t.size()) //匹配成功返回在主串中的位置
return i - t.size() + 1;
else
return -1; //失败返回-1
}
测试文件:main.cpp
[cpp] view
plaincopy
#include "KmpAlgorithm.h"
#include <iostream>
using namespace std;
int main()
{
int pos = 0;
KmpAlgorithm km;
printf("请输入在主串的第几个字符开始匹配:\n");
scanf("%d",&pos);
int position = km.Index_Kmp(pos);
if(position == -1)
printf("在主串中未找到与子串匹配的字符:\n");
else
printf("在主串的第%d个位置匹配成功\n",position);
return 0;
}
相关文章推荐
- Kmp字符匹配算法优化C++实现
- 算法导论-第32章-字符串匹配:Knuth-Morris-Pratt(KMP)算法C++实现
- C++版字符串匹配算法之传统匹配算法加KMP字符串匹配算法
- KMP模式匹配算法 C++实现
- KMP模式匹配算法原理分析、next数组优化及java实现
- KMP模式匹配算法C++实现
- KMP模式匹配算法 C++实现
- 0050 KMP匹配算法的C++实现
- KMP字符串模式匹配算法实现(php)
- 【数据结构&&等差数列】KMP简介和算法的实现(c++ && java)
- <仅是自己做笔记。。。系列15>实现一个挺高级的字符匹配算法: 给一串很长字符串,要求找到符合要求的字符串,例如目的串:123 1******3***2 ,12*****3这些都要找出来
- KMP模式匹配算法中next,nextval的分别实现
- kmp字符匹配算法 c语言
- sunday 字符串匹配算法的实现(支持二进制匹配)
- KMP字符串匹配算法及C语言实现
- KMP模式匹配算法实现
- 算法代码实现之Union-Find,C++实现,quick-find、quick-union、加权quick-union(附带路径压缩优化)
- 实现一个比较高级的字符匹配算法,即一串很长的字符,要求找到符合要求字符的字符串
- KMP模式匹配算法实现
- 最速下降优化算法的C++实现