最大时间复杂度为|P|(|T|-|P|+1)次
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
int match(string T,string P)//返回目标串与模式串完全匹配的第i个位置
{
int p=0;
int t=0;
int plen=P.length();
int tlen=T.length();
if(tlen<plen)
return -1;
while(p<plen&&t<tlen)
{
if(T[t]==P[p])
t++,p++;
else
{
t=t-p+1;
p=0;
}
}
if(p>=plen)
return t-plen+1;
else
return -1;
}
int main()
{
string s1="abcde";
string s2="de";
printf("%d\n",match(s1,s2));
return 0;
}
朴素模式串匹配
最新推荐文章于 2020-10-12 18:02:40 发布