#P3471. [POI 2008] POC-Trains

[POI 2008] POC-Trains

Description

The Trains of Colour Parade begins tomorrow in Byteotia.

Intense preparations are already in progress at the station’s auxiliary tracks. There are nn parallel tracks at the station, numbered from 11 to nn. Train no. ii occupies the ithi^{th} track.

Every train consists of ll cars, and each car is painted with one of 26 colours (denoted by lowercase letters of the English alphabet).

We say that two trains look the same if their corresponding cars are painted the same colour.

Throughout the parade a crane will switch places of certain pairs of cars. The real parade, however, will take place tomorrow.

Today the train dispatcher, Byteasar, watched the general rehearsal closely. He even wrote down the sequence of car swaps.

Byteasar particularly dislikes many trains looking the same.

For each train pp he would like to calculate the maximum number of trains that, at some moment, look the same as train pp at that very moment.

Task

Write a program that:

  • reads the descriptions of the trains occupying the tracks and the sequence of car swaps,
  • for each train determines the maximum number of trains that look the same as it at some moment,
  • writes out the result.

Given nn strings, each of length ll. There are mm operations, each swapping two characters. For each string, find the maximum number of strings that are identical to it at some moment.

Input Format

The first line of input contains three integers nn, ll and mm (2n10002 \le n \le 1000, 1l1001 \le l \le 100, 0m1000000 \le m \le 100\,000), denoting respectively the number of trains, their common length, and the number of car swaps. The following nn lines contain descriptions of the trains on successive tracks.

The kthk^{th} of these lines consists of ll lowercase letters of the English alphabet, denoting the colours of successive cars of the kthk^{th} train. Then mm lines describing the car swaps follow, in the order of the swaps. The rthr^{th} line contains four integers p1p_1, w1w_1, p2p_2, w2w_2 (1p1,p2n1 \le p_1, p_2 \le n, 1w1,w2l1 \le w_1, w_2 \le l, p1p2p_1 \ne p_2 or w1w2w_1 \ne w_2), denoting the rthr^{th} car swap—the car no. w1w_1 of train no. p1p_1 is swapped with the car no. w2w_2 of train no. p2p_2.

Output Format

Your program should write out exactly nn lines. The kthk^{th} line should contain one integer—the number of trains that, at some moment, look the same as train no. kk.

5 6 7
ababbd
abbbbd
aaabad
caabbd
cabaad
2 3 5 4
5 3 5 5
3 5 2 2
1 2 4 3
2 2 5 1
1 1 3 3
4 1 5 6

3
3
3
2
3

Hint

The figure presents the successive car swaps:

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
           (0)       (1)       (2)       (3)       (4)       (5)       (6)       (7)

The number of trains looking the same as either of trains no. 1, 2, or 3 was maximal at time (4) (when all three looked the same). The number of trains looking the same as train no. 5 was maximal at times (5) and (6). The number of trains looking the same as train no. 4 was maximal at time (2).

Translated by ChatGPT 5