KMP字符串匹配
来源:互联网 发布:itv端口居然能上网 编辑:程序博客网 时间:2024/04/30 00:30
#include <iostream>
#include <string>
using namespace std;
string str,tar;
int f[10000+10];
void Fail () //失败函数
{
f[0]=-1;
for(int j=1;j<str.size();j++)
{
int m=f[j-1];
while(str.at(m+1)!=str.at(j)&&m>=0)
m=f[m];
if(str.at(m+1)==str.at(j)) f[j]=m+1;
else f[j]=-1;
}
}
int main ()
{
memset(f,0,sizeof(f));
cin>>tar>>str;
int m=0,n=0;
Fail();
for(int i=0;i<str.size();i++)
cout<<f[i]<<endl;
while(m<tar.size()&&n<str.size())
{
if(tar.at(m)==str.at(n)) { m++; n++; }
else if(n==0) m++;
else n=f[n-1]+1;
}
if(n==str.size()) cout<<m-str.size()<<endl;
else cout<<-1<<endl;
system("pause");
return 0;
}
#include <string>
using namespace std;
string str,tar;
int f[10000+10];
void Fail () //失败函数
{
f[0]=-1;
for(int j=1;j<str.size();j++)
{
int m=f[j-1];
while(str.at(m+1)!=str.at(j)&&m>=0)
m=f[m];
if(str.at(m+1)==str.at(j)) f[j]=m+1;
else f[j]=-1;
}
}
int main ()
{
memset(f,0,sizeof(f));
cin>>tar>>str;
int m=0,n=0;
Fail();
for(int i=0;i<str.size();i++)
cout<<f[i]<<endl;
while(m<tar.size()&&n<str.size())
{
if(tar.at(m)==str.at(n)) { m++; n++; }
else if(n==0) m++;
else n=f[n-1]+1;
}
if(n==str.size()) cout<<m-str.size()<<endl;
else cout<<-1<<endl;
system("pause");
return 0;
}
- KMP 字符串匹配算法
- kmp字符串匹配算法
- kmp字符串匹配算法
- KMP字符串匹配算法
- 字符串匹配算法-kmp
- KMP(字符串匹配)算法
- 字符串匹配 KMP
- KMP 字符串匹配算法
- 字符串匹配算法:KMP
- KMP算法 字符串匹配
- 字符串匹配 KMP 算法
- KMP字符串匹配(1)
- KMP字符串匹配(2)
- KMP字符串匹配(3)
- KMP字符串匹配算法
- KMP字符串匹配
- KMP字符串匹配算法
- KMP字符串匹配
- Oracle 升级线路图
- GetCurrentThread()获取当前线程句柄的陷阱
- IOS5修改UIToolBar 和UINavigationBar 的背景颜色
- 如何在vim里面把空格缩进改为tab缩进
- 黑马程序员_ado.net_连接sql server 总结
- KMP字符串匹配
- VC下的时间
- IOS NSLog 参数配置
- 我对IoC/DI的理解
- eclipse注释
- 股票种类简介
- Hibernate中执行存储过程的方法
- MSP430 AD模块笔记
- 那些年我们一起追寻过的考研(华科计算机)