1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| #include <bits/stdc++.h> #define ll long long #define ld long double #define int long long #define N 10005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> #define x first #define y second using namespace std; int t,n,F[N];; char T[N],S[N];
int KMP_match(char *S,char *T,int *F){ int p = -1,ans = 0; for(int i=0;T[i];i++){ while(p!=-1 && S[p+1]!=T[i]) p = F[p]; if(S[p+1] == T[i]) p += 1; if(!S[p+1]){ ans += 1; p = F[p]; } } return ans; }
void KMP_build(char *S,int *F){ int p = F[0] = -1; for(int i=1;S[i];i++){ while(p!=-1 && S[p+1]!=S[i]) p = F[p]; if(S[p+1] == S[i]) p += 1; F[i] = p; } }
void solve(){ while(n--){ memset(F,0,sizeof(F)); cin>>S; KMP_build(S,F); cout<<KMP_match(S,T,F)<<endl; } }
signed main(){ Orz; cin>>t; while(t--){ cin>>T>>n; solve(); } }
|