結果

問題 No.2761 Substitute and Search
ユーザー winguwingu
提出日時 2024-06-11 12:32:39
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,161 ms / 4,000 ms
コード長 4,944 bytes
コンパイル時間 2,867 ms
コンパイル使用メモリ 264,676 KB
実行使用メモリ 138,880 KB
最終ジャッジ日時 2024-06-11 12:33:01
合計ジャッジ時間 18,115 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1,371 ms
138,752 KB
testcase_05 AC 2,161 ms
138,880 KB
testcase_06 AC 551 ms
138,880 KB
testcase_07 AC 1,510 ms
138,880 KB
testcase_08 AC 537 ms
138,880 KB
testcase_09 AC 1,517 ms
138,880 KB
testcase_10 AC 570 ms
138,880 KB
testcase_11 AC 1,519 ms
138,880 KB
testcase_12 AC 1,080 ms
138,880 KB
testcase_13 AC 1,015 ms
138,880 KB
testcase_14 AC 1,033 ms
138,752 KB
testcase_15 AC 1,061 ms
138,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef DEFINED_ONLY_IN_LOCAL
#include <dump.hpp>
#define dump(...) cpp_dump(__VA_ARGS__)
#else
#undef dump
#define dump(...)
#endif
#define rep1(i, a) for (int i = 0; i < (int)(a); i++)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep3(i, a, b, c) for (int i = (int)(a); i < (int)(b); i += (int)(c))
#define overloadRep(a, b, c, d, e, ...) e
#define rep(...) overloadRep(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define drep(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using ll = long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
const int inf = 1e9;
const ll INF = 1e18;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

struct cincout {
    cincout() {
        ios_base::sync_with_stdio(false);
        cin.tie(nullptr);
        cout << fixed << setprecision(15);
    }
} init;

template <class T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
istream &operator>>(istream &is, vector<T> &v) {
    for (T &in : v) {
        is >> in;
    }
    return is;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    rep(i, (int)v.size()) {
        os << v[i] << " \n"[i + 1 == (int)v.size()];
    }
    return os;
}

template <class T>
istream &operator>>(istream &is, vector<vector<T>> &vv) {
    for (vector<T> &v : vv) {
        is >> v;
    }
    return is;
}

template <class T>
ostream &operator<<(ostream &os, vector<vector<T>> &vv) {
    for (vector<T> &v : vv) {
        os << v;
    }
    return os;
}

template <class T1, class T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
    is >> p.first >> p.second;
    return is;
}

template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
    os << p.first << " " << p.second << '\n';
    return os;
}

bool bit(ll x, int p) {
    return (x >> p) & 1;
}

bool out(int ni, int nj, int h, int w) {
    return (ni < 0 or ni >= h or nj < 0 or nj >= w);
}

int pc(ll x) {
    return __builtin_popcountll(x);
}
#include <atcoder/segtree>
using namespace atcoder;

mt19937_64 r(time(0));
const int mod1 = 1000000007, mod2 = 1000000009;
const int base1 = r() % mod1, base2 = r() % mod2;

using TT = tuple<int, int, int, int>;
TT op(TT l, TT r){
    auto [a, b, c, d] = l;
    auto [e, f, g, h] = r;
    int res1 = ((ll)a * g + e) % mod1;
    int res2 = ((ll)b * h + f) % mod2;
    return TT(res1, res2, ((ll)c * g) % mod1, ((ll)d * h) % mod2);
}
TT e(){
    return TT(0, 0, 1, 1);
}

TT op_(TT l, TT r) {
    auto [a, b, c, d] = l;
    auto [e, f, g, h] = r;
    int res1 = ((ll)e * c + a) % mod1;
    int res2 = ((ll)f * d + b) % mod2;
    return TT(res1, res2, ((ll)c * g) % mod1, ((ll)d * h) % mod2);
}

struct SegmentRollingHash{
    segtree<TT, op, e> seg;
    segtree<TT, op_, e> r_seg;
    bool reverse = false;

    SegmentRollingHash(const string &s = "", bool reverse_ = false) : reverse(reverse_) {
        int n = s.size();
        seg = segtree<TT, op, e>(n);
        if(reverse)r_seg = segtree<TT, op_, e>(n);
        rep (i, n) {
            seg.set(i, TT(s[i], s[i], base1, base2));
            if(reverse)r_seg.set(i, TT(s[i], s[i], base1, base2));
        }
    }

    inline void set(int i, char c) {
        seg.set(i, TT(c, c, base1, base2));
        if(reverse)r_seg.set(i, TT(c, c, base1, base2));
    }

    inline ll get(int l, int r) {
        auto [a, b, c, d] = seg.prod(l, r);
        return (ll)a * mod2 + b;
    }

    inline ll r_get(int l, int r) {
        auto [a, b, c, d] = r_seg.prod(l, r);
        return (ll)a * mod2 + b;
    }
};

int main(){
    int n,l,q;cin>>n>>l>>q;
    vector<string> s(n);cin>>s;
    vector<SegmentRollingHash> rol(n);
    rep(i,n){
        rol[i] = SegmentRollingHash(s[i]);
    }

    while(q--){
        int type;cin>>type;
        if(type==1){
            int k;
            char c, d;
            cin>>k>>c>>d;
            k--;
            rep(i,n){
                if(s[i][k]==c){
                    s[i][k]=d;
                    rol[i].set(k, d);
                }
            }
        }else{
            string t;
            cin>>t;
            int m=t.size();
            ll cor1=0,cor2=0;
            rep(i,m){
                cor1 = cor1 * base1 + t[i];
                cor1 %= mod1;
            }
            rep(i,m){
                cor2 = cor2 * base2 + t[i];
                cor2 %= mod2;
            }

            int ans=0;
            rep(i,n){
                if(rol[i].get(0,m) == cor1 * mod2 + cor2)ans++;
            }

            cout<<ans<<endl;
        }
    }
    return 0;
}
0