QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#236486#2343. First of Her NamePetroTarnavskyiRE 0ms0kbC++172.3kb2023-11-04 00:14:512023-11-04 00:14:51

Judging History

你现在查看的是最新测评结果

  • [2023-11-04 00:14:51]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-11-04 00:14:51]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int AL = 26;

struct Node
{
	int p;
	int c;
	int g[AL];
	int nxt[AL];
	int link;
	
	void init()
	{
		c = -1;
		p = -1;
		fill(g, g + AL, -1);
		fill(nxt, nxt + AL, -1);
		link = -1;
	}
};

struct AC
{
	vector<Node> a;
	int sz;
	void init(int n)
	{
		a.resize(n);
		a[0].init();
		sz = 1;
	}
	int addStr(const string& s)
	{
		int v = 0;
		FOR (i, 0, SZ(s))
		{
			int c = s[i] - 'a'; // change to [0 AL)
			if (a[v].nxt[c] == -1)
			{
				a[v].nxt[c] = sz;
				a[sz].init();
				a[sz].c = c;
				a[sz].p = v;
				sz++;
			}
			v = a[v].nxt[c];
		}
		return v;
	}
	int go(int v, int c)
	{
		if (a[v].g[c] != -1)
			return a[v].g[c];
			
		if (a[v].nxt[c] != -1)
			a[v].g[c] = a[v].nxt[c];
		else if (v != 0)
			a[v].g[c] = go(getLink(v), c);
		else
			a[v].g[c] = 0;
			
		return a[v].g[c];
	}
	int getLink(int v)
	{
		if (a[v].link != -1)
			return a[v].link;
		if (v == 0 || a[v].p == 0)
			return 0;
		return a[v].link=go(getLink(a[v].p), a[v].c);
	}
};

const int N = 1'000'447;

AC A;
int c[N];
VI g[N];
VI g2[N];
int a[N];
int val[N];

void dfs(int v, int x)
{
	x = A.go(x, c[v] - 'A');
	val[x]++;
	for (auto to : g[v])
		dfs(to, x);
}

void dfs2(int v)
{
	for (auto to : g2[v])
	{
		if (to != 0)
		{	
			dfs2(to);
			val[v] += val[to];
		}
	}
}


int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(15);
	
	A.init(N);
	int n, k;
	cin >> n >> k;
	vector<string> v(k);
	FOR (i, 0, n)
	{
		char ch;
		int par;
		cin >> ch >> par;
		c[i + 1] = ch;
		g[par].PB(i + 1);
	}
	FOR (i, 0, k)
	{
		cin >> v[i];
		reverse(ALL(v[i]));
		a[i] = A.addStr(v[i]);
	}
	FOR (i, 0, A.sz)
	{
		g2[A.getLink(i)].PB(i);
	}
	dfs(1, 0);
	dfs2(0);
	FOR (i, 0, k)
	{
		cout << val[a[i]] << '\n';
	}
	cerr << double(clock()) / CLOCKS_PER_SEC << '\n';
	
	return 0;
}


Details

Test #1:

score: 0
Runtime Error