#B4095. Clara's Magic Pocket: Maximum Occurrences of Favorite Number

    ID: 11752 Type: Default 1000ms 256MiB

Clara's Magic Pocket: Maximum Occurrences of Favorite Number

Clara's Magic Pocket: Maximum Occurrences of Favorite Number

Clara has a magical pocket from which she can pull out objects in the form of digits 0 to 9. One day, she finds that her pocket contains several digit-shaped items. Clara has a favorite number \(x\) and she wants to assemble as many copies of \(x\) as possible using the available items. Each item can be used at most once.

There is a twist: the items shaped like 2 and 5 are interchangeable (i.e. a \(2\) can be used as a \(5\) and vice versa), and similarly, the items 6 and 9 are interchangeable. Other digits cannot be substituted for one another.

For example, if Clara's favorite number is 42 and her pocket produces the sequence 23454:

  • She can use the first item (2) and the third item (4) to form 42 (by interpreting the 2 as needed),
  • Alternatively, she can use the fourth item (5, which can serve as a 2) and the fifth item (4) to form 42.

Your task is to determine the maximum number of copies of \(x\) that can be formed using the digits from the pocket.

The replacement rules can be summarized as follows:

  • Digits 2 and 5 are interchangeable. Thus, when forming \(x\), the available count for these digits is \(count(2)+count(5)\) and the required amount is \(freq_x(2)+freq_x(5)\).
  • Digits 6 and 9 are interchangeable. So the available count is \(count(6)+count(9)\) and the required count is \(freq_x(6)+freq_x(9)\).
  • For all other digits, the available count must meet the exact required frequency in \(x\).

The answer is the maximum number of times you can completely form \(x\) with the available items.

inputFormat

The input consists of two lines:

  1. The first line contains the string \(x\), representing Clara's favorite number.
  2. The second line contains a string of digits (each between 0 and 9) representing the items available in the magical pocket.

outputFormat

Output a single integer representing the maximum number of copies of \(x\) that can be formed using the available items.

sample

42
23454
2