#include<stdio.h>#include<iostream>#include<algorithm>#include<string>#include<cstring>
using namespace std;
string s,p;int next_[257];voidgetnext(){int len=p.size();for(int i=0;i<256;i++)next_[i]=-1;for(int i=0;i<len;i++)next_[p[i]]=i;//打next_表,记录模式串相应字符的位置 }intsunday(){int slen=s.size();int plen=p.size();if(slen==0)return-1;//如果s的长度为0,不需要匹配,直接返回-1 for(int i=0;i<slen-plen;){int j=i;//s[j] int k=0;//p[k]for(;k<plen&&j<slen&&s[j]==p[k];j++,k++);//一直匹配,找到失配 j 和 k if(k==plen)//说明已经找到一段匹配串return i;//如果要查找出现次数,改成cnt++else{if(i+plen<slen)i+=(plen-next_[s[i+plen]]);elsereturn-1;// //如果要查找出现次数,改成return cnt}}return-1;}intmain(){
s="I love DNF and Code";
p="love";getnext();if(sunday())printf("you find it at %d\n",sunday());elseprintf("sorry,you do not find it!\n");}