結果

問題 No.430 文字列検索
ユーザー hedwig100hedwig100
提出日時 2020-08-13 13:46:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 2,208 bytes
コンパイル時間 1,566 ms
コンパイル使用メモリ 171,428 KB
実行使用メモリ 7,036 KB
最終ジャッジ日時 2024-04-18 01:30:46
合計ジャッジ時間 2,339 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 13 ms
7,036 KB
testcase_02 AC 12 ms
5,376 KB
testcase_03 AC 10 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 6 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 17 ms
5,776 KB
testcase_12 AC 17 ms
6,276 KB
testcase_13 AC 16 ms
6,288 KB
testcase_14 AC 13 ms
5,408 KB
testcase_15 AC 12 ms
5,376 KB
testcase_16 AC 14 ms
5,376 KB
testcase_17 AC 13 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

template<int char_size,int base>
struct Trie {
    struct Node {
        vector<int> child,accept;
        int c,common;
        Node(int c):c(c),common(0) {
            child.assign(char_size,-1);
        }
    };
    vector<Node> tree;
    int root;
    Trie(): root(0) {
        tree.push_back(Node(root));
    }
    void insert(const string &s,int id) {
        int node_id = 0;
        for (int i = 0; i < (int)s.size(); ++i) {
            int c = (int)(s[i] - base);
            int &next_id = tree[node_id].child[c];
            if (next_id < 0) {
                next_id = (int)tree.size();
                tree.push_back(Node(c));
            }
            ++tree[node_id].common;
            node_id = next_id;
        }
        ++tree[node_id].common;
        tree[node_id].accept.push_back(id);
    }
    // insert s to Trie
    void insert(const string &s) {
        insert(s,tree[0].common);
    }
    // return the number of s in Trie
    int search(const string &s,bool prefix = false) {
        int node_id = 0;
        for (int i = 0;i < (int)s.size(); ++i) {
            int c = (int)(s[i] - base);
            int &next_id = tree[node_id].child[c];
            if (next_id < 0) return 0;
            node_id = next_id;
        }
        return prefix ? 1:(int)tree[node_id].accept.size();
    }
    // if prefix of s in Trie
    bool prefix(const string &s) {
        return search(s,true) > 0;
    }
};

int main() {
    string s; cin >> s;
    int M; cin >> M;
    Trie<26,'A'> tr;
    rep(i,M) {
        string c; cin >> c;
        tr.insert(c);
    }
    ll ans = 0;
    rep(i,s.size()) {
        for (int j = 1; (j <= 10 && i + j <= (int)s.size()); ++j) {
            ans += tr.search(s.substr(i,j));
        }
    }
    cout << ans << '\n';
    return 0;
}
0