結果

問題 No.2761 Substitute and Search
ユーザー srjywrdnprkt
提出日時 2024-10-21 20:30:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 944 ms / 4,000 ms
コード長 1,625 bytes
コンパイル時間 2,449 ms
コンパイル使用メモリ 206,840 KB
最終ジャッジ日時 2025-02-24 22:14:19
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/segtree>
#include <atcoder/modint>

using namespace std;
using ll = long long;
using namespace atcoder;

mt19937_64 rng(time(0));
ll BASE = rng() % 998244353;

//Rolling Hash on Segment Tree

using S = modint998244353;
S op(S a, S b){
    return a+b;
}

S e() {
    return 0;
}

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    /*
       tがSの接頭辞であるか?
       t[1:|T|]=S[1:|T|]
       Rolling Hash
    */

    ll N, L, Q, t, k;
    char c, d;
    string T;
    cin >> N >> L >> Q;
    vector<string> s(N);
    vector<S> p(L);
    
    p[0] = 1;
    for (int j=1; j<L; j++) p[j] = p[j-1] * BASE;
    
    vector<segtree<S, op, e>> tree;
    for (int i=0; i<N; i++){
        cin >> s[i];
        vector<S> v(L);
        for (int j=0; j<L; j++){
            v[j] = p[j] * (s[i][j]-'a');
        }
        tree.push_back(segtree<S, op, e>(v));
    };

    while(Q--){
        cin >> t;
        if (t == 1){
            cin >> k >> c >> d;
            k--;
            for (int i=0; i<N; i++){
                if (s[i][k] == c){
                    s[i][k] = d;
                    tree[i].set(k, p[k]*(d-'a'));
                }
            }
        }
        else{
            cin >> T;
            int ans=0;
            S x=0;
            for (int i=0; i<T.size(); i++){
                x += p[i] * (T[i]-'a');
            }
            for (int i=0; i<N; i++){
                S y = tree[i].prod(0, T.size());
                ans += (x.val() == y.val());
            }
            cout << ans << endl;
        }
    }

    return 0;
}
0