QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#315139#2343. First of Her NamePetroTarnavskyiAC ✓1132ms419028kbC++202.2kb2024-01-27 00:11:342024-01-27 00:11:34

Judging History

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

  • [2024-01-27 00:11:34]
  • 评测
  • 测评结果:AC
  • 用时:1132ms
  • 内存:419028kb
  • [2024-01-27 00:11:34]
  • 提交

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;

	Node(int _c, int _p)
	{
		c = _c;
		p = _p;
		fill(g, g + AL, -1);
		fill(nxt, nxt + AL, -1);
		link = -1;
	}
};

struct AC
{
	vector<Node> a;
	void init(int n)
	{
		a.reserve(n);
		a.PB(Node(-1, -1));
	}
	int addStr(const string& s)
	{
		int v = 0;
		FOR (i, 0, SZ(s))
		{
			// change to [0 AL)
			int c = s[i] - 'A'; 
			if (a[v].nxt[c] == -1)
			{
				a[v].nxt[c] = SZ(a);
				a.PB(Node(c, v));
			}
			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, SZ(A.a))
	{
		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;
}



詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 8024kb

Test #2:

score: 0
Accepted
time: 0ms
memory: 8028kb

Test #3:

score: 0
Accepted
time: 3ms
memory: 8092kb

Test #4:

score: 0
Accepted
time: 3ms
memory: 8092kb

Test #5:

score: 0
Accepted
time: 3ms
memory: 8040kb

Test #6:

score: 0
Accepted
time: 0ms
memory: 8096kb

Test #7:

score: 0
Accepted
time: 3ms
memory: 8256kb

Test #8:

score: 0
Accepted
time: 0ms
memory: 8040kb

Test #9:

score: 0
Accepted
time: 3ms
memory: 8020kb

Test #10:

score: 0
Accepted
time: 3ms
memory: 8024kb

Test #11:

score: 0
Accepted
time: 188ms
memory: 381240kb

Test #12:

score: 0
Accepted
time: 3ms
memory: 7964kb

Test #13:

score: 0
Accepted
time: 0ms
memory: 8024kb

Test #14:

score: 0
Accepted
time: 3ms
memory: 7968kb

Test #15:

score: 0
Accepted
time: 3ms
memory: 7984kb

Test #16:

score: 0
Accepted
time: 0ms
memory: 8192kb

Test #17:

score: 0
Accepted
time: 3ms
memory: 8256kb

Test #18:

score: 0
Accepted
time: 3ms
memory: 8128kb

Test #19:

score: 0
Accepted
time: 3ms
memory: 8000kb

Test #20:

score: 0
Accepted
time: 3ms
memory: 8028kb

Test #21:

score: 0
Accepted
time: 0ms
memory: 8124kb

Test #22:

score: 0
Accepted
time: 0ms
memory: 8124kb

Test #23:

score: 0
Accepted
time: 0ms
memory: 11244kb

Test #24:

score: 0
Accepted
time: 0ms
memory: 11644kb

Test #25:

score: 0
Accepted
time: 0ms
memory: 8572kb

Test #26:

score: 0
Accepted
time: 6ms
memory: 12268kb

Test #27:

score: 0
Accepted
time: 4ms
memory: 8508kb

Test #28:

score: 0
Accepted
time: 0ms
memory: 11636kb

Test #29:

score: 0
Accepted
time: 4ms
memory: 8508kb

Test #30:

score: 0
Accepted
time: 4ms
memory: 8916kb

Test #31:

score: 0
Accepted
time: 7ms
memory: 14112kb

Test #32:

score: 0
Accepted
time: 11ms
memory: 17000kb

Test #33:

score: 0
Accepted
time: 13ms
memory: 22072kb

Test #34:

score: 0
Accepted
time: 104ms
memory: 110128kb

Test #35:

score: 0
Accepted
time: 241ms
memory: 68380kb

Test #36:

score: 0
Accepted
time: 269ms
memory: 276684kb

Test #37:

score: 0
Accepted
time: 183ms
memory: 188912kb

Test #38:

score: 0
Accepted
time: 120ms
memory: 51080kb

Test #39:

score: 0
Accepted
time: 1091ms
memory: 419028kb

Test #40:

score: 0
Accepted
time: 1132ms
memory: 414540kb

Test #41:

score: 0
Accepted
time: 109ms
memory: 98160kb

Test #42:

score: 0
Accepted
time: 228ms
memory: 303168kb

Test #43:

score: 0
Accepted
time: 107ms
memory: 110044kb

Test #44:

score: 0
Accepted
time: 168ms
memory: 143396kb

Test #45:

score: 0
Accepted
time: 210ms
memory: 77016kb

Test #46:

score: 0
Accepted
time: 3ms
memory: 8060kb

Test #47:

score: 0
Accepted
time: 4ms
memory: 8088kb

Test #48:

score: 0
Accepted
time: 0ms
memory: 8160kb

Test #49:

score: 0
Accepted
time: 0ms
memory: 8028kb

Test #50:

score: 0
Accepted
time: 3ms
memory: 8188kb

Test #51:

score: 0
Accepted
time: 3ms
memory: 8060kb

Test #52:

score: 0
Accepted
time: 0ms
memory: 8060kb

Test #53:

score: 0
Accepted
time: 3ms
memory: 11356kb

Test #54:

score: 0
Accepted
time: 4ms
memory: 13968kb

Test #55:

score: 0
Accepted
time: 3ms
memory: 8192kb

Test #56:

score: 0
Accepted
time: 6ms
memory: 8936kb