QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#178164#2343. First of Her NamePetroTarnavskyi#TL 148ms223676kbC++172.5kb2023-09-13 18:49:012023-09-13 18:49:02

Judging History

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

  • [2023-09-13 18:49:02]
  • 评测
  • 测评结果:TL
  • 用时:148ms
  • 内存:223676kb
  • [2023-09-13 18:49:01]
  • 提交

answer

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b) - 1; i >= (a); 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 mod1 = 1'000'000'007;
const int mod2 = 1'000'000'009;

struct Pair
{
	int a, b;
	
	Pair(): a(0), b(0) {}
	Pair(int _a, int _b): a(_a), b(_b) {}
	
	Pair operator +(const Pair& p) const
	{
		int na = a + p.a >= mod1 ? a + p.a - mod1 : a + p.a;
		int nb = b + p.b >= mod2 ? b + p.b - mod2 : b + p.b;
		return Pair(na, nb);
	}
	
	Pair operator -(const Pair& p) const
	{
		int na = a - p.a < 0 ? a - p.a + mod1 : a - p.a;
		int nb = b - p.b < 0 ? b - p.b + mod2 : b - p.b;
		return Pair(na, nb);
	}
	
	Pair operator *(const Pair& p) const
	{
		int na = LL(a) * p.a % mod1;
		int nb = LL(b) * p.b % mod2;
		return Pair(na, nb);
	}
	bool operator <(const Pair& p) const
	{
		return MP(a, b) < MP(p.a, p.b);
	}
	LL toLL() const
	{
		return LL(a) * mod2 + b;
	}
};


const int MAX = 1'000'447;
Pair h[MAX];
Pair pw[MAX + 47];
int c[MAX];
unordered_set<int> len;
unordered_map<LL, int> m;
VI g[MAX];
vector<int> st;
Pair hq[MAX];
Pair p(47, 47);

void dfs(int v)
{
	if (v != 0)
	{
		int x = c[v] - 'A' + 1;
		Pair pr(x, x);
		h[v] = h[st.back()] * p + pr;
		st.PB(v);
		for (auto l : len)
		{
			if (l < SZ(st))
			{
				int i = SZ(st) - l - 1;
				Pair H = pw[MAX];
				Pair hash = (h[v] - h[st[i]] * pw[l]) * H;
				if (m.count(hash.toLL()))
					m[hash.toLL()]++;
			}
		}
	}
	else
	{
		h[v] = Pair(0, 0);
		st.PB(v);
	}
	for (auto to : g[v])
		dfs(to);
	st.pop_back();
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(15);

	pw[0] = Pair(1, 1);
	FOR (i, 1, MAX + 7)
	{
		pw[i] = pw[i - 1] * p;
	}

	int n, k;
	cin >> n >> k;
	len.reserve(474747);
	vector<string> v(k);
	FOR (i, 0, n)
	{
		char ch;
		int par;
		cin >> ch >> par;
		c[i + 1] = ch;
		g[par].PB(i + 1);
	}
	m.reserve(2'474'747);
	FOR (i, 0, k)
	{
		cin >> v[i];
		len.insert(SZ(v[i]));
		
		Pair hs(0, 0);
		FOR (j, 0, SZ(v[i]))
		{
			int x = v[i][j] - 'A' + 1;
			hs = hs + Pair(x, x) * pw[j];
		}
		hs = hs * pw[MAX];
		m[hs.toLL()] = 0;
		hq[i] = hs;
	}
	
	dfs(0);
	
	FOR (i, 0, k)
	{
		cout << m[hq[i].toLL()] << '\n';
	}
	
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 15ms
memory: 77260kb

Test #2:

score: 0
Accepted
time: 16ms
memory: 75256kb

Test #3:

score: 0
Accepted
time: 19ms
memory: 75284kb

Test #4:

score: 0
Accepted
time: 20ms
memory: 75308kb

Test #5:

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

Test #6:

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

Test #7:

score: 0
Accepted
time: 12ms
memory: 77252kb

Test #8:

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

Test #9:

score: 0
Accepted
time: 19ms
memory: 77240kb

Test #10:

score: 0
Accepted
time: 15ms
memory: 75284kb

Test #11:

score: 0
Accepted
time: 148ms
memory: 223676kb

Test #12:

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

Test #13:

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

Test #14:

score: 0
Accepted
time: 16ms
memory: 75256kb

Test #15:

score: 0
Accepted
time: 19ms
memory: 75352kb

Test #16:

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

Test #17:

score: 0
Accepted
time: 19ms
memory: 77276kb

Test #18:

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

Test #19:

score: 0
Accepted
time: 19ms
memory: 75200kb

Test #20:

score: 0
Accepted
time: 8ms
memory: 77340kb

Test #21:

score: 0
Accepted
time: 9ms
memory: 75252kb

Test #22:

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

Test #23:

score: 0
Accepted
time: 18ms
memory: 78968kb

Test #24:

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

Test #25:

score: 0
Accepted
time: 10ms
memory: 75304kb

Test #26:

score: 0
Accepted
time: 22ms
memory: 75524kb

Test #27:

score: 0
Accepted
time: 15ms
memory: 75608kb

Test #28:

score: 0
Accepted
time: 20ms
memory: 77244kb

Test #29:

score: 0
Accepted
time: 19ms
memory: 75416kb

Test #30:

score: 0
Accepted
time: 33ms
memory: 76360kb

Test #31:

score: 0
Accepted
time: 20ms
memory: 76764kb

Test #32:

score: 0
Accepted
time: 24ms
memory: 79848kb

Test #33:

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

Test #34:

score: -100
Time Limit Exceeded