結果

問題 No.430 文字列検索
ユーザー pghk
提出日時 2025-10-11 12:21:52
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 1,099 bytes
コンパイル時間 3,014 ms
コンパイル使用メモリ 281,196 KB
実行使用メモリ 9,240 KB
最終ジャッジ日時 2025-10-11 12:21:56
合計ジャッジ時間 4,029 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i, j, N) for (int i = j; i < N; i++)

struct node {
    int child[26];
    int weight;
};

const int MAX_M = 50009;
const ull B = 1'000'000'007;
string S, C[MAX_M];
int M;

void solve() {
    node init;
    rep(i, 0, 26) init.child[i] = -1;
    init.weight = 0;

    vector<node> trie = {init};
    rep(i, 0, M) {
        int v = 0;
        rep(j, 0, C[i].size()) {
            if (trie[v].child[C[i][j] - 'A'] == -1) {
                trie[v].child[C[i][j] - 'A'] = trie.size();
                trie.push_back(init);
            }
            v = trie[v].child[C[i][j] - 'A'];
        }
        trie[v].weight = 1;
    }

    int ans = 0;
    rep(i, 0, S.size()) {
        int v = 0;
        rep(j, i, S.size()) {
            if (trie[v].child[S[j] - 'A'] == -1) break;
            v = trie[v].child[S[j] - 'A'];
            ans += trie[v].weight;
        }
    }

    cout << ans << endl;
}

int main() {
    cin >> S >> M;
    rep(i, 0, M) cin >> C[i];

    solve();

    return 0;
}
0