結果

問題 No.430 文字列検索
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2017-06-09 18:59:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 2,596 bytes
コンパイル時間 768 ms
コンパイル使用メモリ 73,616 KB
実行使用メモリ 8,316 KB
最終ジャッジ日時 2023-10-23 20:59:18
合計ジャッジ時間 3,218 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 61 ms
8,316 KB
testcase_02 AC 59 ms
4,896 KB
testcase_03 AC 58 ms
4,896 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 55 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 62 ms
6,776 KB
testcase_12 AC 64 ms
7,316 KB
testcase_13 AC 66 ms
7,316 KB
testcase_14 AC 62 ms
6,372 KB
testcase_15 AC 61 ms
5,560 KB
testcase_16 AC 60 ms
5,560 KB
testcase_17 AC 59 ms
5,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) cerr<<(#v)<<":";for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;
#define printVS(vs) cerr<<(#vs)<<":"<<endl;for(auto(s):(vs)){cerr<<(s)<< endl;}
#define printVV(vv) cerr<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;}
#define printP(p) cerr<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cerr<<(#vp)<<":"<<endl;for(auto(p):(vp)){cerr<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cerr << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cerr << first << " "; output(rest...);
}

static const int NUM_CHILDREN = 26;
static const char MIN_CHAR = 'A';

class Trie {
public:
    bool exist;
    Trie *next[NUM_CHILDREN];
    Trie() : exist(false) {
        fill(next, next + NUM_CHILDREN, (Trie*)0);
    }
};

void construct(Trie *trie, const vector<string> &strings) {
    for (auto s : strings) {
        Trie *p = trie;
        for (auto c : s) {
            // cerr << "s = " << s << ", c = " << c << endl;
            int idx = c - MIN_CHAR;
            if (!p->next[idx]) p->next[idx] = new Trie;
            p = p->next[idx];
        }
        p->exist = true;
    }
}

void dump(Trie* p, string s) {
    if (p->exist) cerr << s << endl;
    for (int i = 0; i < NUM_CHILDREN; ++i) {
        if (p->next[i]) {
            dump(p->next[i], s + char(i + MIN_CHAR));
        }
    }
}

void search(Trie* trie, const string &s, int &cnt) {
    // cerr << "s = " << s << endl;
    Trie *p = trie;
    for (auto c : s) {
        int idx = c - MIN_CHAR;
        if (!p->next[idx]) return;
        p = p->next[idx];
        // cerr << "c = " << c << endl;
        if (p->exist) {
            cnt++;
        }
    }
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    string s;
    cin >> s;
    int q;
    cin >> q;
    vector<string> strings(q);
    rep(i, q) cin >> strings[i];

    Trie *trie = new Trie;

    construct(trie, strings);

    // dump(trie, "");

    int cnt = 0;

    rep(i, s.size()) {
        search(trie, s.substr(i), cnt);
    }

    cout << cnt << endl;
}
0