WA 81 求调
查看原帖
WA 81 求调
374769
Epi4any楼主2024/7/15 10:51

rt,被一个绿题搞毛了。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;
const int N = 2e6 + 5;

struct trie
{
	int ch[26], end, fail;
}t[maxn];
int cnt = 1;

int sum[maxn], pos[maxn];

void getFail()
{
	for (int i = 0; i < 26; i++) t[0].ch[i] = 1;
	queue<int> q;
	q.push(1), t[1].fail = 0;
	while (q.size())
	{
		int u = q.front(); q.pop();
		for (int i = 0; i < 26; i++)
		{
			int v = t[u].ch[i], f = t[u].fail;
			if (!v)
			{
				t[u].ch[i] = t[f].ch[i];
				continue;
			}
			t[v].fail = t[f].ch[i];
			q.push(v);
		}
	}
}

struct edge
{
	int v, next;
}e[maxn];
int head[maxn], tot;

inline void addedge(int u, int v)
{
	e[++tot] = (edge){v, head[u]};
	head[u] = tot;
}

void calc(int u)
{
	for (int i = head[u]; i; i = e[i].next)
		calc(e[i].v), sum[u] += sum[e[i].v];
}

int main()
{
	int n;
	char st[N];
	scanf("%s", st + 1);
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		char s[maxn];
    	scanf("%s", s + 1);
		int k = strlen(s + 1), p = 1;
		for (int j = 1; j <= k; j++)
		{
			int c = s[j] - 'a';
			if (!t[p].ch[c]) t[p].ch[c] = ++cnt;
			p = t[p].ch[c];
		}
		t[p].end++, pos[i] = p;
    }
	getFail();
	for (int i = 1; i <= cnt; i++) addedge(t[i].fail, i);
	
	int m = strlen(st + 1), p = 1;
	for (int i = 1; i <= m; i++)
	{
		int c = st[i] - 'a';
		p = t[p].ch[c];
		sum[p]++;
	}
    calc(0);
	
    for (int i = 1; i <= n; i++)
        printf("%d\n", sum[pos[i]]);
	return 0;
}
2024/7/15 10:51
加载中...