#P9943. [USACO21FEB] Year of the Cow B

[USACO21FEB] Year of the Cow B

Problem Description

Farmer John’s cows are very excited to learn that the Year of the Ox is being celebrated recently. The Year of the Ox is always the cows’ favorite.

We know that in the Chinese calendar, the zodiac for each year follows a 1212-year cycle: Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig, Rat (Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig, Rat), and then back to Ox.

Bessie proudly says that she was born in a Year of the Ox many years ago. Her friend Elsie wants to know how many years apart her birth year is from Bessie’s, and she hopes you can help her figure it out by looking at the relationships between the birth years of several cows on the farm.

Input Format

The first line contains an integer NN (1N1001\le N\le 100). The next NN lines each contain a phrase of 88 words, specifying the relationship between the birth years of two cows, in the format

Mildred born in previous Dragon year from Bessie (Mildred was born in the previous Dragon year from Bessie),

or

Mildred born in next Dragon year from Bessie (Mildred was born in the next Dragon year from Bessie).

The last word is the name of a cow on the farm, which is Bessie or a cow that has already appeared earlier in the input.

The first word is the name of a cow on the farm, which is not Bessie and has not appeared earlier in the input. All cow names are at most 1010 characters long and contain only characters a..z or A..Z.

The 55-th word is one of the twelve zodiacs above.

The 44-th word is either previous or next. For example, if the phrase is Mildred born in previous Dragon year from Bessie, then Mildred’s birth year is the closest Dragon year that is strictly before (not equal to) Bessie’s birth year.

Output Format

Output the number of years between Bessie’s and Elsie’s birth years. The input guarantees that the result can be determined from the given information.

4
Mildred born in previous Dragon year from Bessie
Gretta born in previous Monkey year from Mildred
Elsie born in next Ox year from Gretta
Paulina born in next Dog year from Bessie
12

Hint

Sample Explanation 1

In the above input,

  • Elsie was born 1212 years before Bessie.
  • Mildred was born 99 years before Bessie.
  • Gretta was born 1717 years before Bessie.
  • Paulina was born 99 years after Bessie.

Translated by ChatGPT 5