#include <cstdio> #include <iostream> #include <cstring> #include <string> #include <algorithm> using namespace std; #define maxn 400000 + 10 int len, next[maxn], cnt = 0; int L; string s1, s; void getnext() { int i = 0; int j = -1; next[0] = -1; while(i < len) { if(j == -1 || s[i] == s[j]) { i++, j++; next[i] = j; } else j = next[j]; } } void kmp() { int i = 0; int j = 0; cnt = 0; getnext(); while(i < L) { if(j == -1 || s1[i] == s1[j]) { i++; j++; } else j = next[j]; if(j == len) { cnt++; j = next[j]; } } } int main() { cin>>s>>s1; len = s.size(); L = s1.size(); kmp(); cout<<cnt<<endl; system("pause"); return 0; }
时间: 2024-11-08 03:24:23