字符串哈希,记录每种长度下包含的相应字符串的双哈希值,每次询问时枚举+查询即可,可以用 map<int,set<pair<ll,ll>>> 维护

StringHash a;

void solve() {
    int n, m;
    cin >> n >> m;
    map<int, set<pair<ll, ll>>> mp;

    auto f = [&](const string &s) {
        a.work(s);
        auto hash = a.getHash(1, s.length());
        mp[s.length()].insert(hash);
    };
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        f(s);
    }
    for (int i = 0; i < m; i++) {
        int op;
        string t;
        cin >> op >> t;
        if (op == 1) {
            f(t);
        } else {
            int ans = 0;
            a.work(t);
            for (auto &[x, y] : mp) {
                for (int j = 1; j + x - 1 <= t.length(); j++) {
                    auto hash = a.getHash(j, j + x - 1);
                    if (y.count(hash)) ans++;
                }
            }
            cout << ans << '\n';
        }
    }
}