样例没过求调
查看原帖
样例没过求调
852144
Loser_Syx楼主2024/7/21 08:43

谢谢。

#include <iostream>
#include <cassert>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <random>
#include <ctime>
#include <map>
#include <set>
using namespace std;
// #define int long long
#define pii pair<int, int>
#define eb emplace_back
#define F first
#define S second
#define test(x) cout << "Test: " << x << '\n'
#define lowbit(x) (x & -x)
#define debug puts("qwq");
#define open(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define close fclose(stdin);fclose(stdout);
namespace FastIO {
	template <typename T = int>
	inline T read() {
		T s = 0, w = 1;
		char c = getchar();
		while (!isdigit(c)) {
			if (c == '-') w = -1;
			c = getchar();
		}
		while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
		return s * w;
	}
	template <typename T>
	inline void read(T &s) {
		s = 0;
		int w = 1;
		char c = getchar();
		while (!isdigit(c)) {
			if (c == '-') w = -1;
			c = getchar();
		}
		while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
		s = s * w;
	}
	template <typename T, typename... Arp> inline void read(T &x, Arp &...arp) {
		read(x), read(arp...);
	}
	template <typename T>
	inline void write(T x, char ch = '\n') {
		if (x < 0) x = -x, putchar('-');
		static char stk[25];
		int top = 0;
		do {
			stk[top++] = x % 10 + '0', x /= 10;
		} while (x);
		while (top) putchar(stk[--top]);
		putchar(ch);
		return;
	}
	template <typename T>
	inline void smax(T &x, T y) {
		if (x < y) x = y;
	}
	template <typename T>
	inline void smin(T &x, T y) {
		if (x > y) x = y;
	}
	void quit() {
		exit(0);
	}
} using namespace FastIO;
const int N = 2e5 + 19;
int t[N][26], fail[N], ans[N], num[N], kp[N], cnt = 1, q[N], H = 1, T, d[N], r[N], nowans[N];
char s[N * 10];
void go(int id) {
	int len = strlen(s), u = 1;
	for (int i = 0; i < len; ++i) {
		if (!t[u][s[i] - 'a']) t[u][s[i] - 'a'] = ++cnt;
		u = t[u][s[i] - 'a'];
	}
	if (!num[u]) num[u] = id;
	r[id] = num[u];
}
void add() {
	int len = strlen(s);
	for (int i = 0, u = 1; i < len; ++i) {
		u = t[u][s[i] - 'a'];
		++ans[u];
	}
} void find_fail() {
	for (int i = 0; i < 26; ++i) t[0][i] = 1;
	q[++T] = 1; fail[1] = 0;
	while (H <= T) {
		int u = q[H++];
		int f = fail[u];
		for (int i = 0; i < 26; ++i) {
			int v = t[u][i];
			if (!v) {
				t[u][i] = t[f][i];
				continue;
			}
			fail[v] = t[f][i];
			++d[t[f][i]];
			q[++T] = v;
		}
	}
} void go_topo() {
	H = 1, T = 0;
	for (int i = 1; i <= cnt; ++i) if (!d[i]) q[++T] = i;
	while (H <= T) {
		int u = q[H++];
		nowans[num[u]] = ans[u];
		int f = fail[u];
		ans[f] += ans[u];
		if (!--d[u]) q[++T] = f;
	}
}
signed main() {
	int n = read();
	for (int i = 1; i <= n; ++i) {
		scanf("%s", s);
		go(i);
	} find_fail();
	scanf("%s", s);
	add();
	go_topo();
	for (int i = 1; i <= n; ++i) write(nowans[r[i]]);
	return 0;
}
2024/7/21 08:43
加载中...