結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-05-16 14:28:23 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 59 ms / 2,000 ms |
| コード長 | 1,791 bytes |
| コンパイル時間 | 3,581 ms |
| コンパイル使用メモリ | 112,112 KB |
| 実行使用メモリ | 8,576 KB |
| 最終ジャッジ日時 | 2024-11-10 01:10:56 |
| 合計ジャッジ時間 | 4,020 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, j, n) for (ll i = j; i < (n); ++i)
#define rrep(i, j, n) for (ll i = (n) - 1; j <= i; --i)
#define all(a) a.begin(), a.end()
template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &a) {
for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? " " : "") << a[i];
return os << '\n';
}
template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &a) {
for (T &x : a) { is >> x; }
return is;
}
template <int set_size = 26, char base_char = 'a'>
class trie {
struct Node {
Node *next[set_size] = {nullptr};
bool isleaf = false;
};
public:
trie() : nodes(1, new Node) {} // add the root node
int ans = 0;
bool search(const std::string &s) {
Node *now = nodes[0];
for (const char &c : s) {
int i = c - base_char;
ans += now->isleaf;
if (!now->next[i]) { return false; }
now = now->next[i];
}
ans += now->isleaf;
return now->isleaf;
}
void insert(const std::string &s) {
Node *now = nodes[0];
for (const char &c : s) {
int i = c - base_char;
if (!now->next[i]) {
nodes.push_back(new Node);
now->next[i] = nodes.back();
}
// now->next[c]->count++;
now = now->next[i];
}
now->isleaf = true;
}
private:
std::vector<Node *> nodes;
};
int main() {
cin.tie(0)->sync_with_stdio(0);
string s, t;
int n;
cin >> s >> n;
trie<26, 'A'> trie;
for (int i = 0; i < n; ++i) {
cin >> t;
trie.insert(t);
}
for (int i = 0; i < (int)s.size(); ++i)
trie.search(s.substr(i, (int)s.size() - i));
cout << trie.ans << endl;
}