Four problems where a map keeps something for every key: copies of a word, copies of a point, the birth years of cows with names, and the counts inside a window that slides along an array.
- Counting copies is
cnt[key]++; asking about a word that was never added must print 0, so look it up withcountorfind. - When the key is a point, make it a
pair<int, int>:cnt[{x, y}]. - Year of the Cow: one map gives each animal its place in the 12-year cycle
(Ox is 0), another gives each cow her birth year counted from Bessie's (0).
"Previous" means stepping back year by year — at least one step — until the
year's animal matches. For a negative year
y, the animal's place is(y % 12 + 12) % 12. Print the distance between the two years, without a sign. - In the window problem, erase a key when its count drops to 0, and print
cnt.size()once the window is full —n - k + 1numbers on one line.