Block Game
Time limit2sMemory limit512 MB
For each letter, find the minimum number of blocks needed so any choice of one word per board can be spelled at once.
- Level
Medium5 of 10
- Topics
- Brute force, Bit manipulation
- Solved
- No attempts yet
Problem
Farmer John wants to teach his cows to read, so he prepares spelling boards of the kind used with preschoolers (). Each board has one word and one picture on each of its two sides. One side might show the word 'cat' with a picture of a cat, and the other side the word 'dog' with a picture of a dog. When the boards lie on the ground, words face upward. Flipping some of the boards exposes a different set of words.
To help the cows spell, Farmer John wants to carve wooden blocks, each embossed with a single letter of the alphabet. He wants enough blocks of every letter that, whichever set of words faces upward, the cows can lay out all of those words at once. For example, if and the upward words are 'box', 'cat', and 'car', the cows need one 'b' block, one 'o' block, one 'x' block, two 'c' blocks, two 'a' blocks, one 't' block, and one 'r' block.
For each of the 26 letters, find the minimum number of blocks Farmer John has to provide so that the cows can spell every visible word no matter which side each board shows.
Input
The first line contains the integer .
Each of the next lines contains two words separated by a space, the two words written on the opposite sides of one board. Each word is a string of at most 10 lowercase letters.
Output
Print 26 lines. The first line holds the number of 'a' blocks needed, the second line the number of 'b' blocks needed, and so on through 'z'.
Hint
With 3 boards there are possible sets of upward words. For boards whose sides are (fox, box), (dog, cat), and (car, bus), the eight sets are:
- fox dog car
- fox dog bus
- fox cat car
- fox cat bus
- box dog car
- box dog bus
- box cat car
- box cat bus
The blocks have to be enough to spell all three words in every one of the eight cases.