benchmarks.wiki / Public workspace

Humanity's Last Code Exam / 2019_G / First of Her Name

Problem

Answer published by the source. Consult the official source to check your work against its answer.

question title

First of Her Name

question content

## Problem Description In the Royal Family, names are very important! As the Royal Historian, you have been charged with analyzing the patterns in the names of the Royal Ladies in the realm. There have been nn Royal Ladies, for convenience numbered from 1 to nn. The name of each Lady is an uppercase letter concatenated with the name of her mother. The exception is the Lady numbered 1, the founder of the Royal Family, whose name is just a single uppercase letter. For example, ENERYS could be the mother of AENERYS (as the name AENERYS consists of the single uppercase letter ‘A’ concatenated with ENERYS, which is her mother’s name). Similarly, AENERYS could be the mother of DAENERYS and YAENERYS. You are given the description of all the Royal Ladies. Your task is to determine, for certain interesting strings ss, the number of Royal Ladies for whom ss is a prefix of their name. For example, consider **Sample Input 1** below, with a Royal Line that goes straight from the founder **S** to **AENERYS** (through **YS**, **RYS**, **ERYS**, **NERYS**, and **ENERYS**), with each Lady having exactly one daughter. Then **AENERYS** has two daughters—**DAENERYS** and **YAENERYS**, with the latter having one daughter, **RYAENERYS**. In such a family, **RY** is a prefix of the names of two ladies: **RYS** and **RYAENERYS**. **E** is a prefix of the names of **ERYS** and **ENERYS**. **N** is a prefix only of **NERYS**’s name, while **S** is a prefix only of the name of the founder, **S**. **AY** is not a prefix of any Royal Lady’s name. ## Input The first line of input contains two integers nn and kk, where nn (1n1061 \leq n \leq 10^6) is the total number of Royal Ladies and kk (1k1061 \leq k \leq 10^6) is the number of query strings. Then follow nn lines describing the Royal Ladies. The iith of these lines describes the Royal Lady numbered ii, and contains an uppercase letter cic_i (‘A’–‘Z’) and an integer pip_i, where cic_i is the first letter of the name of Lady ii, and pip_i (p1=0p_1 = 0 and 1pi<i1 \leq p_i < i for i>1i > 1) is the number of her mother (or 0, in the case of the First Lady). All the names are unique. The remaining kk lines each contain one nonempty query string, consisting only of uppercase letters. The sum of the lengths of the query strings is at most 10610^6. ## Output Output kk lines, with the iith line containing the number of Royal Ladies who have the iith query string as a prefix of their name. ## Sample Input 1
Plain-text mathematical notation (without MathML)
## Problem Description

In the Royal Family, names are very important! As the Royal Historian, you have been charged with analyzing the patterns in the names of the Royal Ladies in the realm.

There have been n Royal Ladies, for convenience numbered from 1 to n. The name of each Lady is an uppercase letter concatenated with the name of her mother. The exception is the Lady numbered 1, the founder of the Royal Family, whose name is just a single uppercase letter.

For example, ENERYS could be the mother of AENERYS (as the name AENERYS consists of the single uppercase letter ‘A’ concatenated with ENERYS, which is her mother’s name). Similarly, AENERYS could be the mother of DAENERYS and YAENERYS.

You are given the description of all the Royal Ladies. Your task is to determine, for certain interesting strings s, the number of Royal Ladies for whom s is a prefix of their name.

For example, consider **Sample Input 1** below, with a Royal Line that goes straight from the founder **S** to **AENERYS** (through **YS**, **RYS**, **ERYS**, **NERYS**, and **ENERYS**), with each Lady having exactly one daughter. Then **AENERYS** has two daughters—**DAENERYS** and **YAENERYS**, with the latter having one daughter, **RYAENERYS**.

In such a family, **RY** is a prefix of the names of two ladies: **RYS** and **RYAENERYS**. **E** is a prefix of the names of **ERYS** and **ENERYS**. **N** is a prefix only of **NERYS**’s name, while **S** is a prefix only of the name of the founder, **S**. **AY** is not a prefix of any Royal Lady’s name.

## Input

The first line of input contains two integers n and k, where n (1≤n≤10⁶) is the total number of Royal Ladies and k (1≤k≤10⁶) is the number of query strings.

Then follow n lines describing the Royal Ladies. The ith of these lines describes the Royal Lady numbered i, and contains an uppercase letter c_(i) (‘A’–‘Z’) and an integer p_(i), where c_(i) is the first letter of the name of Lady i, and p_(i) (p₁=0 and 1≤p_(i)<i for i>1) is the number of her mother (or 0, in the case of the First Lady). All the names are unique.

The remaining k lines each contain one nonempty query string, consisting only of uppercase letters. The sum of the lengths of the query strings is at most 10⁶.

## Output

Output k lines, with the ith line containing the number of Royal Ladies who have the ith query string as a prefix of their name.

## Sample Input 1

Original LaTeX notation
## Problem Description

In the Royal Family, names are very important! As the Royal Historian, you have been charged with analyzing the patterns in the names of the Royal Ladies in the realm.

There have been \(n\) Royal Ladies, for convenience numbered from 1 to \(n\). The name of each Lady is an uppercase letter concatenated with the name of her mother. The exception is the Lady numbered 1, the founder of the Royal Family, whose name is just a single uppercase letter.

For example, ENERYS could be the mother of AENERYS (as the name AENERYS consists of the single uppercase letter ‘A’ concatenated with ENERYS, which is her mother’s name). Similarly, AENERYS could be the mother of DAENERYS and YAENERYS.

You are given the description of all the Royal Ladies. Your task is to determine, for certain interesting strings \(s\), the number of Royal Ladies for whom \(s\) is a prefix of their name.

For example, consider **Sample Input 1** below, with a Royal Line that goes straight from the founder **S** to **AENERYS** (through **YS**, **RYS**, **ERYS**, **NERYS**, and **ENERYS**), with each Lady having exactly one daughter. Then **AENERYS** has two daughters—**DAENERYS** and **YAENERYS**, with the latter having one daughter, **RYAENERYS**.

In such a family, **RY** is a prefix of the names of two ladies: **RYS** and **RYAENERYS**. **E** is a prefix of the names of **ERYS** and **ENERYS**. **N** is a prefix only of **NERYS**’s name, while **S** is a prefix only of the name of the founder, **S**. **AY** is not a prefix of any Royal Lady’s name.

## Input

The first line of input contains two integers \(n\) and \(k\), where \(n\) (\(1 \leq n \leq 10^6\)) is the total number of Royal Ladies and \(k\) (\(1 \leq k \leq 10^6\)) is the number of query strings.

Then follow \(n\) lines describing the Royal Ladies. The \(i\)th of these lines describes the Royal Lady numbered \(i\), and contains an uppercase letter \(c_i\) (‘A’–‘Z’) and an integer \(p_i\), where \(c_i\) is the first letter of the name of Lady \(i\), and \(p_i\) (\(p_1 = 0\) and \(1 \leq p_i < i\) for \(i > 1\)) is the number of her mother (or 0, in the case of the First Lady). All the names are unique.

The remaining \(k\) lines each contain one nonempty query string, consisting only of uppercase letters. The sum of the lengths of the query strings is at most \(10^6\).

## Output

Output \(k\) lines, with the \(i\)th line containing the number of Royal Ladies who have the \(i\)th query string as a prefix of their name.

## Sample Input 1

Code

10 5
S 0
Y 1
R 2
E 3
N 4
E 5
A 6
D 7
Y 7
R 9
RY
E
N
S
AY

## Sample Output 1

Code

2
2
1
1
0

platform

atcoder

Discussion

Discussion

No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Source and history

Official source

initial import