UberOA #24
Command Frequency Counter
Estimated EasyJudge-ready
Topics (AI-labelled)HashingSimulation
Each entry is one of cmd1, cmd2, cmd3, or !j, which means executing the command represented by the j-th entry. Count how many times each of the three commands is ultimately executed over all entries. Valid input on this site guarantees that every reference chain eventually reaches an actual command; forward and backward references are allowed, but there are no reference cycles.
This site's standard input/output is stated explicitly.
Input
The first line contains n, followed by n command tokens, 1≤n≤100000 (this site's range); the j in !j is 1..n.
Output
Output the execution counts of cmd1, cmd2, and cmd3, separated by spaces.
Example 1
Input:
7
cmd1
cmd2
cmd3
!1
!2
cmd3
cmd1
Output:
3 2 2
Example 2
Input:
3
!3
!1
cmd2
Output:
0 3 0
Example 3
Input:
1
cmd3
Output:
0 0 1
Example notes
Example 1: cmd1, cmd2, and cmd3 are executed 3, 2, and 2 times respectively. Example 2: the first two entries both eventually reference the third entry, cmd2, so the output is 0 3 0. Example 3: there is only one cmd3, so the output is 0 0 1.
Editorial
Loading solution…