kmp算法
来源:互联网 发布:淘宝企业店铺如何过户 编辑:程序博客网 时间:2024/05/18 02:29
#include <iostream>
#include <string>
using namespace std;
//关键的问题是要理解 该方法是获得下次比较的字符位置 0位置是一个特殊值
//当前位置的值是根据相邻的字符获得的与本身无关
void GetNext( string& s, int iArr[] )
{
const int iLen = s.length();
iArr[0] = -1;
int i = 0;
int j = -1;
while( i < iLen )
{
if( j == -1 || s[i] == s[j] )
{
++i;
++j;
iArr[i] = j;
}
else
{
j = iArr[j];
}
}
}
//获得字串位置
int GetPos( string& s, string sSub )
{
int iArr[100];
GetNext( sSub, iArr );
const int iLen = s.length();
const int iSub = sSub.length();
int i = 0;
int j = 0;
while( i < iLen && j < iSub )
{
if( s[i] == sSub[j] )
{
++i;
++j;
}
else
{
j = iArr[j];
if( j == -1 )
{
++i;
j = 0;
}
}
}
if( j >= iSub && i <= iLen )
{
return i - iSub;
}
return -1;
}
void main()
{
int iArr[14];
string s = "abcdabcefgcda";
string sSub = "gcda";
int iPos = GetPos( s, sSub );
cout << iPos << endl;
}
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- KMP算法
- 左连接、右连接和全连接的区别
- 11.1 存储班长信息的学生类
- Jersey+Spring构建RESTful Web服务
- CCESocket解读
- EasyRec推荐系统常用错误
- kmp算法
- leetcode第一刷_Populating Next Right Pointers in Each Node II
- A Comparison Between Haar Wavelet Transform and Fast Fourier Transform in Analyzing Financial Time S
- Mysql常用命令
- “错误15023:当前数据库中已存在用户或角色”
- Google BigTable介绍
- HDU 1007 Quoit Design 分治法
- 用State模式减少if..elseif语句
- InetAddress.getLocalHost()详解