結果
問題 |
No.430 文字列検索
|
ユーザー |
![]() |
提出日時 | 2017-06-09 18:59:27 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 62 ms / 2,000 ms |
コード長 | 2,596 bytes |
コンパイル時間 | 649 ms |
コンパイル使用メモリ | 73,812 KB |
実行使用メモリ | 8,192 KB |
最終ジャッジ日時 | 2024-11-10 00:15:34 |
合計ジャッジ時間 | 1,904 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 14 |
ソースコード
#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; }