結果

問題 No.2761 Substitute and Search
ユーザー ypwwypww
提出日時 2024-05-17 23:18:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,404 bytes
コンパイル時間 5,020 ms
コンパイル使用メモリ 276,852 KB
実行使用メモリ 659,840 KB
最終ジャッジ日時 2024-05-17 23:18:29
合計ジャッジ時間 13,528 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 MLE -
testcase_07 MLE -
testcase_08 AC 183 ms
20,992 KB
testcase_09 AC 366 ms
21,120 KB
testcase_10 MLE -
testcase_11 MLE -
testcase_12 MLE -
testcase_13 MLE -
testcase_14 AC 214 ms
21,120 KB
testcase_15 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;

#define rep(i, a, b) for(ll i=a; i<b; i++)
#define rrep(i, a, b) for(ll i=a; i>=b; i--)
#define all(a) (a).begin(), (a).end()
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define YesNo(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}

template<typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template<typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); }

inline int popcount(int n) { return __builtin_popcount(n); } // 2進数で表した場合に立ってるビット数がいくつか
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int ctz(int n) { return n != 0 ? __builtin_ctz(n) : -1; } // 2進数で表した場合に 1 の位からいくつ 0 が連なっているか
inline int ctz(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int clz(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; } // 2進数で表した場合に左側にいくつ 0 を埋める必要があるか
inline int clz(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }

const double PI = 3.141592653589793;
const vector<int> DX = { 1, 0, -1, 0 };
const vector<int> DY = { 0, 1, 0, -1 };
const long long INF = 4004004003104004004LL; // (int)INF = 1010931620;
ll coordinate(ll h, ll w, ll W){ return h*W + w; } // 二次元座標を一次元座標に変換

#define endl "\n" // インタラクティブの時はコメントアウトする

vector<vector<char>> g(3001, vector<char>(26));

class TrieNode {
public:
    unordered_map<char, TrieNode*> child;
    long long count;
    TrieNode() : count(0) {}
};

class Trie {
public:
    TrieNode* root;
    Trie() {
        root = new TrieNode();
    }

    void insert(const string& chars) {
        TrieNode* par = root;
        for (char c : chars) {
            if (par->child.find(c) == par->child.end()) {
                par->child[c] = new TrieNode();
            }
            par = par->child[c];
            par->count++;
        }
    }

    int solve(string& s){
        TrieNode* par = root;
        int res = 0;
        int depth = 0;
        queue<pair<TrieNode*, int>> q;
        q.push({root, 0});
        
        while (!q.empty()) {
            auto [node, depth] = q.front(); q.pop();
            if (depth==s.size()){
                res += node->count;
                continue;
            }
            char c = s[depth];
            for (auto& childPair : node->child) {
                if (g[depth][childPair.first-'a'] == c) q.push({childPair.second, depth + 1});
            }
        }
        return res;
    }

};

int main() {
    int n, l, q;
    cin >> n >> l >> q;
    
    Trie trie;
    rep(i,0,n){
        string s;
        cin>>s;
        trie.insert(s);
    }
    rep(i,0,3001)rep(j,0,26) g[i][j] = char('a'+j);

    while(q--){
        int type;
        cin>>type;
        if (type==1){
            int k;
            cin>>k;
            char c,d;
            cin>>c>>d;
            k--;
            g[k][c-'a'] = d;
        }else{
            string s;
            cin>>s;
            cout << trie.solve(s) << endl;
        }
    }
    
    return 0;
}
0