signed

QiShunwang

“诚信为本、客户至上”

暴力字符串匹配

2020/12/26 13:57:56   来源:

无需多讲时间复杂度为O(n*m);

#include<iostream>
#include<cstring>
#include<algorithm>
#include<sstream>
using namespace std;
int index(string s,string p){
	int i=0,j=0;
	while(i<s.size()&&j<p.size()){
		if(s[i]==p[j]){
			++i;
			j++;
		}
		else{
			i=i+1;
			j=0;
		}
	}
	if(j>=p.size()){
		return i-p.size();
	}
	else{
	    return 0;
	}
}
int main()
{
	string s="oiipqwe";
	string p="qwe";
	cout<<index(s,p)<<endl;
	return 0;
 }