KMP
啥也不说了
上代码:
#include <cstdio> #include <iostream> #include <string> #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; #define MAXN 100010 char a[MAXN],b[MAXN]; int next[MAXN],f[MAXN]; int n,m,ans; int main () { cin>>b;//长串 cin>>a;//短串 n=strlen(a); m=strlen(b); next[1]=0; for (int i=2,j=0;i<=n;i++) { while (j>0&&a[i]!=a[j+1]) { j=next[j]; } if (a[i]==a[j+1]) j++; next[i]=j; } for (int i=1,j=0;i<=m;i++) { while (j>0&&(j==n||b[i]!=a[j+1])) j=next[j]; if (b[i]==a[j+1]) j++; f[i]=j; if (f[i]==n-1) ans++; } cout<<ans<<endl; return 0; }