Created
December 1, 2025 13:32
-
-
Save hikariyo/fc6e7653df1b8b82ca2ca384f6b673de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <bits/stdc++.h> | |
| using namespace std; | |
| #define pb push_back | |
| const int N = 2e5+10; | |
| int tr[N][26], fail[N], idx; | |
| vector<int> g[N]; | |
| int node[N], cnt[N]; | |
| int insert(const string& s) { | |
| int u = 0; | |
| for (char c: s) { | |
| int p = c - 'a'; | |
| if (!tr[u][p]) tr[u][p] = ++idx; | |
| u = tr[u][p]; | |
| } | |
| return u; | |
| } | |
| void build() { | |
| queue<int> q; | |
| for (int i = 0; i < 26; i++) if (tr[0][i]) q.push(tr[0][i]); | |
| while (q.size()) { | |
| int u = q.front(); q.pop(); | |
| for (int i = 0; i < 26; i++) { | |
| if (tr[u][i]) { | |
| fail[tr[u][i]] = tr[fail[u]][i]; | |
| q.push(tr[u][i]); | |
| } | |
| else tr[u][i] = tr[fail[u]][i]; | |
| } | |
| } | |
| for (int i = 1; i <= idx; i++) g[fail[i]].pb(i); | |
| } | |
| void match(const string& s) { | |
| int u = 0; | |
| for (char c: s) { | |
| int p = c - 'a'; | |
| u = tr[u][p]; | |
| cnt[u]++; | |
| } | |
| } | |
| void dfs(int u) { | |
| for (int to: g[u]) { | |
| dfs(to); | |
| cnt[u] += cnt[to]; | |
| } | |
| } | |
| int n; | |
| signed main() { | |
| ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); | |
| cin >> n; | |
| for (int i = 1; i <= n; i++) { | |
| string t; | |
| cin >> t; | |
| node[i] = insert(t); | |
| } | |
| build(); | |
| string s; | |
| cin >> s; | |
| match(s); | |
| dfs(0); | |
| for (int i = 1; i <= n; i++) { | |
| cout << cnt[node[i]] << '\n'; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment