Trains
Time limit1sMemory limit128 MB
After each of m car swaps, track the largest number of trains that ever shared each train's exact colour string.
- Level
Medium7 of 10
- Topics
- Hash map, String, Simulation
- Solved
- No attempts yet
Problem
The Trains of Colour Parade opens tomorrow in Byteotia, and the crews are already busy on the station's auxiliary tracks. The station has parallel tracks numbered from to , and train number stands on track . Every train is made of cars, and each car is painted in one of colours, written as a lowercase letter of the English alphabet. Two trains look the same when their cars match colour for colour in every position.
During the rehearsal a crane keeps swapping pairs of cars. The dispatcher watched the whole rehearsal and wrote down the sequence of swaps. He dislikes seeing many identical trains, so for every train he wants to know the largest number of trains (train included) that look the same as at one and the same moment.
Write a program that:
- reads the initial trains and the sequence of car swaps,
- for each train finds the maximum number of trains that look the same as it at some single moment,
- prints those numbers.
Input
The first line holds three integers , , and (, , ): the number of trains, their common length, and the number of car swaps. Each of the next lines holds a string of lowercase letters; the -th of these lines gives the colours of the cars of the train on track , from the first car to the last. Each of the following lines describes one swap, given in the order the swaps happen. A swap line holds four integers , , , (, , with or ): car of train is exchanged with car of train .
Output
Print exactly lines. The -th line holds one integer: the maximum number of trains (train included) that look the same as train at some single moment, taken over the initial arrangement and every arrangement right after a swap.
Note
The diagram below shows the cars being swapped step by step. The numbers through mark the initial state and the state right after each swap.
time (0) (1) (2) (3) (4) (5) (6) (7)
track 1 ababbd ababbd ababbd ababbd aaabbd aaabbd aaabbd aaabbd
track 2 abbbbd ababbd ababbd aaabbd aaabbd acabbd acabbd acabbd
track 3 aaabad aaabad aaabad aaabbd aaabbd aaabbd aaabbd aaabbd
track 4 caabbd caabbd caabbd caabbd cabbbd cabbbd cabbbd dabbbd
track 5 cabaad cabbad caabbd caabbd caabbd aaabbd aaabbd aaabbc
For trains , , and the largest matching group appears at time , when all three read aaabbd. Train reaches its maximum at times and . Train reaches its maximum at time , when tracks and both read caabbd.