const int N = 2e5 + 5;
int n, ans[N];
struct ACAM {
struct node {
int to[26], fail;
vector<int> id;
} T[N];
int tot = 0;
void insert(const string& s, int id) {
int u = 0;
for (char c : s) {
int& v = T[u].to[c-'a'];
if (!v) v = ++tot;
u = v;
}
T[u].id.pb(id);
}
void build() {
queue<int> Q;
for (int c = 0; c < 26; c++)
if (T[0].to[c]) Q.emplace(T[0].to[c]);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int c = 0; c < 26; c++) {
int& v = T[u].to[c];
int x = T[T[u].fail].to[c];
if (!v) {v = x; continue;}
T[v].fail = x, Q.emplace(v);
}
}
}
int f[N];
void run(const string& t) {
int u = 0;
for (char c : t) u = T[u].to[c-'a'], f[u]++;
}
vector<int> e[N];
void solve() {
for (int u = 1; u <= tot; u++)
e[T[u].fail].pb(u);
auto dfs = [&](auto&& dfs, int u) -> void {
for (int v : e[u]) dfs(dfs, v), f[u] += f[v];
for (int i : T[u].id) ans[i] += f[u];
};
dfs(dfs, 0);
}
} AC;
void _main() {
cin >> n;
for (int i = 1; i <= n; i++) {
string s; cin >> s;
AC.insert(s, i);
}
AC.build();
string t; cin >> t; AC.run(t), AC.solve();
for (int i = 1; i <= n; i++) cout << ans[i] << '\n';
} 剪贴板
剪贴板 pn69vf83
剪贴板信息
剪贴板信息
- 发布
- 收录
- 更新