KMP算法以及简单应用(查找单词)
2007-03-07 15:12
501 查看
上学期在重新学数据结构时,看到匹配算法时,看不懂,然后没时间去研究它,暂时放了下来,早段时间重新研究了 经典的KMP算法,终于搞懂了,自己写了一个kmp算法,并利用它进行了简单的应用,查找单词;例如
Input A string as the Object string..
sdfgdsgsdgr
Input A string as the search string..
sd
Total Count Is ..2
0
7
Press any key to continue
源文件cpp文件:
#include"NewKmp.h"
void main()
{
/* string str = "aaaaaaa15";
int next[10];
KMP_Next(str,next);
for(int i = 0;i<str.size();i++)
{
cout<<(str.c_str())[i];
}
cout<<endl;
for(i = 0;i<str.size();i++)
{
cout<<next[i];
}
cout<<endl;*/
string S,P;
int Count;
vector<int> Vec_Pos;
cout<<"Input A string as the Object string.."<<endl;
cin>>S;
cout<<"Input A string as the search string.."<<endl;
cin>>P;
/* KMP(S,P,Pos);
cout<<"The Position Is.. "<<Pos<<endl;*/
Count_Word(S,P,Count,Vec_Pos);
cout<<"Total Count Is .."<<Count<<endl;
for(int i = 0;i<Count;i++)
{
cout<<Vec_Pos[i]<<endl;
}
}
头文件NewKmp.h
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void KMP_Next(const string &str,int next[])
{
int size = str.size();
int j = 1;
int k = 0;
next[0] = 0;
next[1] = 0;
while(j<size)
{
if((str.c_str())[j] == (str.c_str())[k])
{
next[j+1] = k+1;
j++;
k = next[j];
}
else
{
while(k>0) //递推查找
{
k = next[k];
}
if((str.c_str())[j] != (str.c_str())[k]) //是否与首字符相等
{
next[j+1] = 0;
j++;
}
}
}
}
void KMP(const string &S,const string &P,int &Pos)
{
int j = 0;
int i = 0;
int next[10];
KMP_Next(P,next);
bool UPDATE = false;
while(j<S.size())
{
if((S.c_str())[j] == (P.c_str())[i])
{
if(i == P.size()-1)
{
Pos = j-P.size()+1;
UPDATE = true;
break;
}
j++;
i++;
}
else
{
if(i == 0)
{
j++;
}
else
{
i = next[i];
}
}
}
if(UPDATE == false)
{
Pos = -1;
}
}
void Count_Word(const string &S,const string &C,int &Count,vector<int> &Vec_Pos)
{
int i = 0;
Count = 0; //初始化Count
int Pos = -1; //初始化
int Pre_Pos = 0; //记住前一次的位置
string t_str = S; //用于循环S查找串的临时变量
while(i<S.size())
{
t_str = t_str.substr(i); //剪接
KMP(t_str,C,Pos); //KMP算法
if(Pos>=0)
{
Count++;
Vec_Pos.push_back(Pre_Pos+Pos); //记录位置
i = Pos+C.size();
Pre_Pos = i+Pre_Pos; //定位下一次
}
else
{
break;
}
}
}
Input A string as the Object string..
sdfgdsgsdgr
Input A string as the search string..
sd
Total Count Is ..2
0
7
Press any key to continue
源文件cpp文件:
#include"NewKmp.h"
void main()
{
/* string str = "aaaaaaa15";
int next[10];
KMP_Next(str,next);
for(int i = 0;i<str.size();i++)
{
cout<<(str.c_str())[i];
}
cout<<endl;
for(i = 0;i<str.size();i++)
{
cout<<next[i];
}
cout<<endl;*/
string S,P;
int Count;
vector<int> Vec_Pos;
cout<<"Input A string as the Object string.."<<endl;
cin>>S;
cout<<"Input A string as the search string.."<<endl;
cin>>P;
/* KMP(S,P,Pos);
cout<<"The Position Is.. "<<Pos<<endl;*/
Count_Word(S,P,Count,Vec_Pos);
cout<<"Total Count Is .."<<Count<<endl;
for(int i = 0;i<Count;i++)
{
cout<<Vec_Pos[i]<<endl;
}
}
头文件NewKmp.h
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void KMP_Next(const string &str,int next[])
{
int size = str.size();
int j = 1;
int k = 0;
next[0] = 0;
next[1] = 0;
while(j<size)
{
if((str.c_str())[j] == (str.c_str())[k])
{
next[j+1] = k+1;
j++;
k = next[j];
}
else
{
while(k>0) //递推查找
{
k = next[k];
}
if((str.c_str())[j] != (str.c_str())[k]) //是否与首字符相等
{
next[j+1] = 0;
j++;
}
}
}
}
void KMP(const string &S,const string &P,int &Pos)
{
int j = 0;
int i = 0;
int next[10];
KMP_Next(P,next);
bool UPDATE = false;
while(j<S.size())
{
if((S.c_str())[j] == (P.c_str())[i])
{
if(i == P.size()-1)
{
Pos = j-P.size()+1;
UPDATE = true;
break;
}
j++;
i++;
}
else
{
if(i == 0)
{
j++;
}
else
{
i = next[i];
}
}
}
if(UPDATE == false)
{
Pos = -1;
}
}
void Count_Word(const string &S,const string &C,int &Count,vector<int> &Vec_Pos)
{
int i = 0;
Count = 0; //初始化Count
int Pos = -1; //初始化
int Pre_Pos = 0; //记住前一次的位置
string t_str = S; //用于循环S查找串的临时变量
while(i<S.size())
{
t_str = t_str.substr(i); //剪接
KMP(t_str,C,Pos); //KMP算法
if(Pos>=0)
{
Count++;
Vec_Pos.push_back(Pre_Pos+Pos); //记录位置
i = Pos+C.size();
Pre_Pos = i+Pre_Pos; //定位下一次
}
else
{
break;
}
}
}
相关文章推荐
- 编写一个程序,从标准输入中读取若干string对象并查找连续重复出现的单词。所谓连续重复出现的意思是:一个单词后面紧跟着这个单词本身。要求记录连续重复出现的最大次数以及对应的单词
- gulp开发简单配置以及配合browserify应用
- PHP扩展开发与内核应用阅读笔记---php的作用域以及如何在扩展中定义,查找php变量
- 关于记事本的“查找下一个”以及“向上向下”,C#.net的一个简单实现
- QT 简单应用 单词查询
- Docker 的介绍 ,安装以及简单应用
- Linux下正则表达式以及几种工具:grep,sed,awk,cut,sort,uniq的简单应用
- coreseek(sphinx)的简单应用以及在LAMP中的注意事项
- yii2 basic 框架的初层应用以及简单的增删改查
- 双向循环链表简单的插入、删除、修改以及查找功能的实现
- 简单的Junit和Maven结合应用(Maven项目怎么进行Junit单元测试以及常见问题解决方法)
- 二分查找及简单应用
- 储存每一个单词W以及W的所有前缀,特定方的方向执行一次扫描的时候,如果被查找的单词作为前缀不在散列表中,那么在这个方向上可以及早终止
- RecycleView简单应用以及上拉刷新
- 链表的头文件以及一些简单的应用
- php手册里的 显示所有你查找的字符的位置,以及该字符所在的单词
- 顺序表的实现以及简单的 插入,删除,查找,输出操作
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- JavaScript调试工具以及简单应用
- 【数据结构】栈的编写以及栈的简单应用