QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#178362#2343. First of Her NamePetroTarnavskyiTL 111ms176972kbC++172.3kb2023-09-13 21:41:222023-09-13 21:41:23

Judging History

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

  • [2023-09-13 21:41:23]
  • 评测
  • 测评结果:TL
  • 用时:111ms
  • 内存:176972kb
  • [2023-09-13 21:41:22]
  • 提交

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;
	
	Pair(): a(0) {}
	Pair(int _a): a(_a) {}
	
	Pair operator +(const Pair& p) const
	{
		int na = a + p.a >= mod1 ? a + p.a - mod1 : a + p.a;
		return Pair(na);
	}
	
	Pair operator -(const Pair& p) const
	{
		int na = a - p.a < 0 ? a - p.a + mod1 : a - p.a;
		return Pair(na);
	}
	
	Pair operator *(const Pair& p) const
	{
		int na = LL(a) * p.a % mod1;
		return Pair(na);
	}
	int toLL() const
	{
		return a;
	}
};


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

void dfs(int v)
{
	if (v != 0)
	{
		int x = c[v] - 'A' + 1;
		Pair pr(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);
		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);
	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);
		FOR (j, 0, SZ(v[i]))
		{
			int x = v[i][j] - 'A' + 1;
			hs = hs + Pair(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';
	}
	cerr << double(clock()) / CLOCKS_PER_SEC << '\n';
	
	return 0;
}

Details

Test #1:

score: 100
Accepted
time: 7ms
memory: 43980kb

Test #2:

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

Test #3:

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

Test #4:

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

Test #5:

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

Test #6:

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

Test #7:

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

Test #8:

score: 0
Accepted
time: 5ms
memory: 43820kb

Test #9:

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

Test #10:

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

Test #11:

score: 0
Accepted
time: 111ms
memory: 176972kb

Test #12:

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

Test #13:

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

Test #14:

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

Test #15:

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

Test #16:

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

Test #17:

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

Test #18:

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

Test #19:

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

Test #20:

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

Test #21:

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

Test #22:

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

Test #23:

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

Test #24:

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

Test #25:

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

Test #26:

score: 0
Accepted
time: 14ms
memory: 46196kb

Test #27:

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

Test #28:

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

Test #29:

score: 0
Accepted
time: 14ms
memory: 44180kb

Test #30:

score: 0
Accepted
time: 23ms
memory: 44940kb

Test #31:

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

Test #32:

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

Test #33:

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

Test #34:

score: -100
Time Limit Exceeded