結果
| 問題 | No.430 文字列検索 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-12-04 01:10:34 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 34 ms / 2,000 ms |
| + 880µs | |
| コード長 | 4,457 bytes |
| 記録 | |
| コンパイル時間 | 3,107 ms |
| コンパイル使用メモリ | 353,904 KB |
| 実行使用メモリ | 18,048 KB |
| 最終ジャッジ日時 | 2026-12-04 01:10:42 |
| 合計ジャッジ時間 | 5,639 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge4_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 18 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
#define all(a) (a).begin(), (a).end()
#define sz(a) ssize(a)
template <class T, class U> bool chmin(T &a, U b) { return a > b ? a = b, 1 : 0; }
template <class T, class U> bool chmax(T &a, U b) { return a < b ? a = b, 1 : 0; }
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;
#define _O(_1, _2, _3, n, ...) n
#define _r1(i, n) for (ll i = 0; i < (ll)(n); i++)
#define _r2(i, l, r) for (ll i = (ll)(l); i < (ll)(r); i++)
#define rep(...) _O(__VA_ARGS__, _r2, _r1)(__VA_ARGS__)
#define _rr1(i, n) for (ll i = (ll)(n) - 1; i >= 0; i--)
#define _rr2(i, l, r) for (ll i = (ll)(r) - 1; i >= (ll)(l); i--)
#define rrep(...) _O(__VA_ARGS__, _rr2, _rr1)(__VA_ARGS__)
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
rep(i, sz(v)) o << (i ? " " : "") << v[i];
return o;
}
template <class H, class... T> void out(const H &h, const T &...t) {
cout << h;
((cout << " " << t), ...);
#ifdef LOCAL
cout << endl;
#else
cout << '\n';
#endif
}
struct SuffixAutomaton {
struct Node {
int len = 0, link = -1, end = -1,
cnt = 0; // end: 出現右端, cnt: 接頭辞ノードなら 1 (DP初期値)
map<ll, int> nxt;
};
vector<Node> t = {{}};
int last = 0, n = 0;
ll distinct = 0;
SuffixAutomaton() = default;
SuffixAutomaton(const auto &s) {
t.reserve(2 * sz(s) + 2);
for (ll c : s) extend(c);
}
int extend(ll c) {
int cur = sz(t), p = last;
t.push_back({t[last].len + 1, 0, ++n, 1, {}});
for (; ~p && !t[p].nxt.count(c); p = t[p].link) t[p].nxt[c] = cur;
if (~p) {
int q = t[p].nxt[c];
if (t[p].len + 1 == t[q].len) t[cur].link = q;
else {
int cl = sz(t);
t.push_back({t[p].len + 1, t[q].link, t[q].end, 0, t[q].nxt});
for (; ~p && t[p].nxt[c] == q; p = t[p].link) t[p].nxt[c] = cl;
t[q].link = t[cur].link = cl;
}
}
return distinct += t[cur].len - t[t[cur].link].len, last = cur;
}
// notebook:extension-begin queries
vector<int> topo_order() const {
vector<int> b(n + 1), o(sz(t));
for (auto &nd : t) b[nd.len]++;
rep(i, 1, n + 1) b[i] += b[i - 1];
rrep(i, sz(t)) o[--b[t[i].len]] = i;
return o; // len 昇順 (逆順で suffix link 木 DP)
}
vll build_cnt() const {
auto o = topo_order();
vll c(sz(t));
rep(i, sz(t)) c[i] = t[i].cnt;
rrep(i, 1, sz(o)) c[t[o[i]].link] += c[o[i]];
return c;
}
int walk(const auto &p) const {
int v = 0;
for (ll c : p) {
auto it = t[v].nxt.find(c);
if (it == t[v].nxt.end()) return -1;
v = it->second;
}
return v;
}
void step(int &v, int &len, ll c) const {
while (~v && !t[v].nxt.count(c)) v = t[v].link, len = ~v ? t[v].len : 0;
~v ? (v = t[v].nxt.at(c), len++) : (v = len = 0);
}
array<ll, 4> lcs(const auto &u) const {
int v = 0, len = 0;
array<ll, 4> best = {0, 0, 0, 0};
rep(i, sz(u)) {
step(v, len, u[i]);
if (len > best[1] - best[0]) best = {t[v].end - len, t[v].end, i + 1 - len, i + 1};
}
return best;
}
// notebook:extension-end queries
// notebook:extension-begin kth_substr
vll kth_substring(ll k) const {
auto o = topo_order();
vll dp(sz(t), 1);
dp[0] = 0;
rrep(i, sz(o)) for (auto [c, v] : t[o[i]].nxt) dp[o[i]] = min(INF, dp[o[i]] + dp[v]);
if (k < 0 || k >= dp[0]) return {};
vll res;
int u = 0;
while (true) {
if (u && !k--) return res;
for (auto [c, v] : t[u].nxt) {
if (k < dp[v]) {
res.push_back(c), u = v;
break;
}
k -= dp[v];
}
}
}
// notebook:extension-end kth_substr
};
int main(){
std::cin.tie(nullptr), std::ios_base::sync_with_stdio(false);
string t; cin >> t;
SuffixAutomaton SA(t);
auto c = SA.build_cnt();
ll q; cin >> q;
ll sm = 0;
while(q--){
string p; cin >> p;
auto u = SA.walk(p);
sm += (~u?c[u]:0);
}
out(sm);
}
// g++ -std=c++23 -DLOCAL tmp.cpp
// ./a.out