結果

問題 No.430 文字列検索
ユーザー ebi_flyebi_fly
提出日時 2021-08-24 16:08:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 5,840 bytes
コンパイル時間 3,785 ms
コンパイル使用メモリ 194,176 KB
実行使用メモリ 8,500 KB
最終ジャッジ日時 2024-04-26 23:16:01
合計ジャッジ時間 4,799 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 9 ms
8,500 KB
testcase_02 AC 5 ms
6,940 KB
testcase_03 AC 5 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 9 ms
6,944 KB
testcase_12 AC 8 ms
6,944 KB
testcase_13 AC 8 ms
6,944 KB
testcase_14 AC 8 ms
6,944 KB
testcase_15 AC 6 ms
6,940 KB
testcase_16 AC 7 ms
6,940 KB
testcase_17 AC 7 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>
#include <limits>
#include <cassert>
#include <bitset>
#include <chrono>
#include <random>
#include <memory>
#include <atcoder/all>

/* macro */
 
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (i64 i = (i64)(a); i< (i64)(n); i++)
#define RRep(i, a, n) for (i64 i = ((i64)(n-i64(1))); i>=(i64)(a); i--)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define Bit(n) (1LL<<(n))

/* macro end */

/* template */

namespace ebi {

#ifdef LOCAL
#define debug(...)                                                    \
    std::cerr << "LINE: " << __LINE__ << "  [" << #__VA_ARGS__ << "]:", \
        debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
 
void debug_out() { std::cerr << std::endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
    std::cerr << " " << h;
    if (sizeof...(t) > 0) std::cout << " :";
    debug_out(t...);
}
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
    return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
    for (std::size_t i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    return os;
}

std::ostream& operator<<(std::ostream& os, atcoder::modint1000000007 a){
    return os << a.val();
}

std::ostream& operator<<(std::ostream& os, atcoder::modint998244353 a){
    return os << a.val();
}

using size_t = std::size_t;
using i64 = long long;
using u64 = unsigned long long;

template<class T> void fill(std::vector<T> &v) { for(T &e: v) std::cin >> e; }

template<class T>
struct Edge {
    int to;
    T cost;
    Edge(int to, T cost) : to(to), cost(cost) { }
};

template<class T>
using Graph = std::vector<std::vector<Edge<T>>>;

using graph = std::vector<std::vector<int>>;

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

template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    else return gcd(b, a%b);
}

template <class T>
T lcm(T a, T b) {
    return (a*b)/gcd(a,b);
}

template<class T>
T pow(T x, i64 n) {
    T res = 1;
    while(n>0){
        if(n&1) res = res *x;
        x = x*x;
        n>>=1;
    }
    return res;
}

template<class T>
T mod_pow(T x, i64 n, i64 mod) {
    T res = 1;
    while(n > 0) {
        if(n & 1) res = (res * x)%mod;
        x = (x*x)%mod;
        n >>= 1;
    }
    return res;
}

template<class T>
T scan() {
    T val;
    std::cin >> val;
    return val;
}
 
constexpr i64 LNF = std::numeric_limits<i64>::max()/4;

constexpr int INF = std::numeric_limits<int>::max()/4;

const long double PI = acos(-1);

const std::vector<int> dy = {1,0,-1,0,1,1,-1,-1};
const std::vector<int> dx = {0,1,0,-1,1,-1,1,-1};

}

/* template end */

namespace ebi {

namespace internal {

template<int char_size>
struct trie_node {
    int common;
    int nxt[char_size];
    std::vector<int> accept;
    trie_node() : common(0) {
        memset(nxt, -1, sizeof(nxt));
    }
};

}

template<int char_size, int margin>
struct trie {
private:
public:
    trie() {
        nodes.emplace_back(Node());
    }

    void add(const std::string &str) {
        int idx = 0;
        for(const auto &c: str) {
            int val = c - margin;
            if(nodes[idx].nxt[val] < 0) {
                nodes[idx].nxt[val] = int(nodes.size());
                nodes.emplace_back(Node());
            }
            idx = nodes[idx].nxt[val];
            nodes[idx].common++;
        }
        nodes[idx].accept.emplace_back(nodes[0].common++);
    }

    template<class F>
    void query(const std::string &str, int start, F func) const {
        int idx = 0;
        for(int i = start; i < int(str.size()); ++i) {
            int val = str[i] - margin;
            int nxt = nodes[idx].nxt[val];
            if(nxt < 0) {
                return;
            }
            idx = nxt;
            for(const auto id: nodes[idx].accept) {
                func(id);
            }
        }
        return;
    }

    bool search(const std::string &str, int start, bool prefix = false) const {
        int idx = 0;
        for(int i = start; i < int(str.size()); ++i) {
            int val = str[i] - margin;
            int nxt = nodes[idx].nxt[val];
            if(nxt < 0) {
                return -1;
            }
            idx = nxt;
        }
        return prefix ? true : (nodes[idx].accept.size() > 0);
    }

    int size() const {
        return int(nodes.size());
    }
private:
    using Node = internal::trie_node<char_size>;
    std::vector<Node> nodes;
};

}

namespace ebi {

using ld = long double;

using mint = atcoder::modint998244353;

void main_() {
    std::string s;
    int m;
    std::cin >> s >> m;
    trie<26, 'A'> tri;
    rep(i,0,m) {
        std::string c;
        std::cin >> c;
        tri.add(c);
    }
    int n = s.size();
    int ans = 0;
    rep(i,0,n) {
        tri.query(s, i, [&](int id) -> void { ans++; return; });
    }
    std::cout << ans << std::endl;
}

}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t = 1;
    while(t--) {
        ebi::main_();
    }
    return 0;
}
0