結果

問題 No.430 文字列検索
ユーザー coindarwcoindarw
提出日時 2023-04-25 22:30:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 3,141 bytes
コンパイル時間 2,364 ms
コンパイル使用メモリ 209,720 KB
実行使用メモリ 12,448 KB
最終ジャッジ日時 2024-04-27 06:54:29
合計ジャッジ時間 3,063 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 17 ms
12,448 KB
testcase_02 AC 11 ms
6,940 KB
testcase_03 AC 10 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 9 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 19 ms
8,148 KB
testcase_12 AC 19 ms
7,764 KB
testcase_13 AC 21 ms
9,048 KB
testcase_14 AC 16 ms
7,764 KB
testcase_15 AC 15 ms
8,544 KB
testcase_16 AC 14 ms
8,280 KB
testcase_17 AC 13 ms
8,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define bitrep(i, n) for (int i = 0, i##_len = (1 << (int)(n)); i < i##_len; i++)
#define bitrrep(i, n) for (int i = (1 << (int)(n)) - 1ll; i >= 0; i--)
constexpr int inf = 2000'000'000;
constexpr ll linf = 4000000000000000000ll;
constexpr ll M7 = 1000000007ll;
constexpr ll M09 = 1000000009ll;
constexpr ll M9 = 998244353ll;
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
template <typename T>
inline ostream &operator<<(ostream &os, vector<T> &v) {
    for (auto &e : v) os << e << " ";
    return os;
}
template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) noexcept {
    return os << "(" << p.first << ", " << p.second << ")";
}

template <int char_size, int base>
class Trie {
   private:
    struct Node {
        int next[char_size];
        vector<int> accept;  // 末端がこの頂点になる単語のword_idx
        int c;               // str[i] - base
        int common;          // 頂点を共有する単語数
        Node(int c) : c(c), common(0) { rep(i, char_size) next[i] = -1; }
    };
    vector<Node> nodes;

   public:
    Trie() { nodes.push_back(Node(0)); }
    void insert(const string &word, int word_idx) {
        int node_id = 0;
        for (char c_ : word) {
            int c = c_ - base;
            if (nodes[node_id].next[c] == -1) {
                nodes[node_id].next[c] = int(nodes.size());
                nodes.push_back(Node(c));
            }
            ++nodes[node_id].common;
            node_id = nodes[node_id].next[c];
        }
        ++nodes[node_id].common;
        nodes[node_id].accept.push_back(word_idx);
    }
    void insert(const string &word) { insert(word, nodes[0].common); }
    bool search(const string &word, bool prefix = false) {
        int node_id = 0;
        for (char c_ : word) {
            int c = c_ - base;
            if (nodes[node_id].next[c] == -1)
                return false;
            node_id = nodes[node_id].next[c];
        }
        return prefix || (nodes[node_id].accept.size() > 0);
    }
    bool start_with(const string &prefix) { return search(prefix, true); }
    int word_count() const { return nodes[0].common; }
    int node_size() const { return nodes.size(); }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s;
    int m;
    cin >> s >> m;
    vector<string> v(m);
    rep(i, m) cin >> v.at(i);

    Trie<56, 'A'> trie;
    rep(i, m) trie.insert(v.at(i));
    int ans = 0;
    reps(len, 10) {
        rep(i, s.size() - len + 1) {
            string t = s.substr(i, len);
            if (trie.search(t))
                ans++;
        }
    }
    cout << ans << "\n";
    return 0;
}
0