USACO 2021 February Contest, BronzeProblem 1. Year of the Cow

Keywords: regex

Title Description

Farmer John's cows were very excited when they learned that they were celebrating the year of the ox recently. The year of the ox is always the favorite of dairy cows.

We know that the zodiac corresponding to each year in the Chinese calendar follows a 12-year cycle: ox, tiger, rabbit, dragon, snake, horse, pig, rat (ox, tiger, rabbit, dragon, snake, horse, sheep, monkey, chicken, dog, pig, mouse), and then back to the ox.

Bessie, a cow, proudly said that she was born in the year of the ox many years ago. Her friend Elsie wants to know how many years her birth is different from Bessie, and hopes you can help her calculate by looking at the relationship between the birth years of several cows on the farm.

Input format (read from terminal / standard input):

The first line of input contains an integer   N(1≤N≤100). following   N lines each line contains an 8-word phrase that specifies the relationship between the birth years of the two cows in the format

"Mildred born in previous Dragon year from Bessie",


"Mildred 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 in the previous input.

The first word is the name of a cow on the farm, which is not "Bessie" and has not appeared in the previous input. All cow names shall not exceed 10 characters and contain only the characters A..Z or A..Z.

The fifth word is one of the above twelve zodiac animals.

The fourth word is one of "previous" or "next". For example, if the phrase is "Mildred born in previous Dragon year from Bessie", the year of birth of Mildred is the year of the Dragon closest to and strictly before (not equal to) the year of birth of Bessie.

Output format (output to terminal / standard output):

Output the number of years between the birth years of Bessie and Elsie. Input guarantees that the results can be obtained from the given information.

Input example:

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

Output example:


In the above input,

  • Elsie was born 12 years before Bessie.
  • Mildred was born nine years before Bessie.
  • Gretta was born 17 years before Bessie.
  • Paulina was born nine years before Bessie.

Question: Brian Dean

Train of thought analysis

1. Requirements: it is known that Bessie was born in the year of the ox. analyze n sentences. Each sentence describes the relationship between the birth years of the two people and the zodiac of the first person. Calculate the difference between the birth years of Bessie and Elsie and output it

2. Suppose Bessie was born in 2021, store the information in the map, and then parse n sentences, including the year relationship between A and B. according to the relationship before and after birth, assume the birth year of Niuniu A, and continuously calculate whether its year is A given sign forward or backward

3. Since the birth year of Niuniu A is the closest and strictly before or after the birth year of Niuniu B (not equal to), it only needs to be completed within 11 times forward or backward to determine the birth year of Niuniu a, and N is 100 at most

Distribution of test points and thinking process of doing questions:

1. About test points:

        a. For the first test point, in n sentences, the first name of sentence i is guaranteed to be the last name of sentence i-1

        b. At the later test point, the first name of the i-th sentence may be the name of any year calculated in the previous i-1 sentence.

        c. At the beginning, I was trapped in it, thinking about whether to make another map to record the name and year, and call the forward number, but the official website parsed which_ The ANI function is solved, which is equivalent to calculating every year from 2021. This operation is really wonderful~

2. Problems encountered in the process of thinking and writing:

        a. Assuming Bessie was born in 2021, with the ruler of this year, it is easier to calculate the years of other cattle

        b. Read in multiple useless strings and consider naming them with their original names

        c. map key value pairs can make the string also be used as the array subscript

Knowledge points:

Simulation, string, branch loop and map will be more convenient to write

The following is the code part (the commented part is the debugging part, which is very important)

#include <iostream>
#include <map>
#include <string>
#include <cstdio>
using namespace std;
string animals[12] = {"Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig", "Rat"}; 
map <string, int> when_born;//Key value pairs for name and year of birth 

//Judge which sign the current year is
//Methods: from the year 2021 to the year 2021, count the year's zodiac
string which_ani(int year){
	//x is the subscript of animals where y = the zodiac in 2021 
	int x = 0, y = 2021;
	//If the current year is before year y, the subscript should also be moved left until y==year 
	while (y > year){
		if (x == -1) x = 11; 
	//If the current year is after year y, the subscript year must be shifted to the right
	while (y < year) {
		if (x == 12) x = 0;
	return animals[x];

int main(){
//	freopen("", "r", stdin); 
	int n;
	cin >> n;
//	cout << n << endl; 
	string name1, born, in, relation, Zodiac, year, from, name2;
	when_born["Bessie"] = 2021;//Bessie was born in 2021
	for (int i = 0; i < n; i++){
		cin >> name1 >> born >> in >> relation >> Zodiac >> year >> from >> name2;
		when_born[name1] = when_born[name2];
		if (relation == "previous")when_born[name1]--;
		else when_born[name1]++;
//		Cout < < year of name1 before output calculation < < when_ born[name1] << endl;
		//Judge the current when_ Does the born [name1] year reach the Zodiac sign 
		while(which_ani(when_born[name1]) != Zodiac) {
//			cout << "wawa" << endl;
			if (relation == "previous")when_born[name1]--;
			else when_born[name1]++;
//		Cout < < name1 < < year of birth < < when_ born[name1] << endl; 
//	cout << when_born["Bessie"] << " " << when_born["Elsie"] << endl; 
	int diff = when_born["Bessie"] - when_born["Elsie"];
	if (diff <= 0) cout << -diff;
	else cout << diff;
	return 0;

Thinking and harvest:

1. The author of USACO is a real God. The topic has rich background, fits the reality and is very fun;

2. The first question of the third monthly competition is really not simple. The difficulty of the second question and above of domestic CSP-J is more difficult than that of the first two monthly competitions

3. STLyyds, the analysis given on the official website is also very good. This solution only adds some explanation to the official website program, strong!

Posted by scliburn on Wed, 06 Oct 2021 20:19:50 -0700