Box Factory (Large)
Time limit5sMemory limit512 MB
Match boxes and toys of equal type in order on run-length encoded lines to maximize the number of pairs.
- Level
Medium7 of 10
- Topics
- Dynamic programming, String matching
- Solved
- No attempts yet
Problem
You own a factory with two assembly lines. The first line makes boxes and the second line makes toys to put in those boxes. Every box and every toy carries a type number, and a toy fits a box only when the two type numbers are equal.
You start by picking up a box from the first line and a toy from the second line. After that you repeat the following actions as often as you like.
- Throw out the box you are holding and pick up the next box.
- Throw out the toy you are holding and pick up the next toy.
- If the box and the toy have the same type, put the toy in the box, send it out to customers, then pick up the next box and the next toy.
You always pick boxes up in the order in which they are made, and the same holds for toys. You know the order in which both lines produce. Plan a strategy that sends out as many boxed toys as possible, and report that largest number.
The two lines make a lot of boxes and toys. They tend to make one kind of thing for a long time before switching.
Input
The first line of input contains the number of test cases .
Each test case begins with a line containing two integers and . The next line contains integers , and the line after that contains integers .
The first assembly line makes boxes of type , then boxes of type , and so on until it finishes with boxes of type . The second assembly line makes toys of type through toys of type in the same way. Two neighbouring runs may share a type number.
Limits
Output
For each test case, output one line in the form Case #x: y, where is the test case number starting from 1 and is the largest number of boxed toys you can send out to customers.