#include <iostream>
#include <string>
using namespace std;
struct KMP{
string base, sub;
int sub_len, base_len;
int next[1000];
void build_base(){
cin>>base;
base_len = base.size();
}
void build_sub(){
cin>>sub;
sub_len = sub.size();
make_next();
}
void make_next(){
int i = 1, j = 0;
next[0] = -1;
while(j<sub_len){
if(i == -1|| sub[i]==sub[j]) next[++j] = ++i;
else i = next[i];
}
}
int solve() {
int i = 0, j = 0;
int ans = 0;
for(i; i < base_len; ++i) {
while(j > 0&& base[i]!=sub[j])
j = next[j];
if(base[i]==sub[j]) ++j;
if(j == sub_len) ++ans;
}
return ans;
}
}kmp;
int main(){
kmp.build_base();
kmp.build_sub();
cout<<kmp.solve()<<endl;
system("pause");
return 0;
}
KMP学长讲课
最新推荐文章于 2022-06-02 15:18:30 发布