解题思路:哈希模板


#include <bits/stdc++.h>
#define ull unsigned long long
using namespace std;
const int maxn = 2e6+7;
const int mod = 1e5+7;
const ull base = 123;
char t[maxn];
ull h[maxn], has[maxn];
int head[mod], tot = 0;
 
struct Node{
    ull v;
    int nxt;
}e[maxn];
 
void add(ull x){
    int pos = x%mod;
    e[tot].v = x;
    e[tot].nxt = head[pos];
    head[pos] = tot++;
}
 
int Find(ull x){
    int pos = x%mod;
    for(int i = head[pos]; ~i; i = e[i].nxt){
        if(e[i].v == x)return 1;
    }
    return 0;
}
 
int main(){
    scanf("%s",t+1);
    int l = strlen(t+1);
    memset(head, -1, sizeof head);
    for(int i = 1; i <= l; i++)
        t[i+l] = t[i];
    h[0] = 1;
    for(int i = 1; i < maxn; i++){
        h[i]=h[i-1]*base;
       // if(h[i]<h[i-1]) cout << "yes" << endl;
    }
    has[0] = 0;
    for(int i = 1; i <= 2*l; i++){
        has[i] = has[i-1]*base+t[i];
        if(i>=l) add(has[i]-has[i-l]*h[l]);
    }
    int T;scanf("%d",&T);
    while(T--){
        scanf("%s",t+1);
        int len = strlen(t+1), ans = 0;
        for(int i = 1; i <= len; i++){
            has[i] = has[i-1]*base + t[i];
            if(i>=l) ans += Find(has[i]-has[i-l]*h[l]);
        }
        printf("%d\n",ans);
    }
    return 0;
}