D - 回転暗号の解読 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400 点

問題文

高橋君は、N 通の暗号文を受信しました。各暗号文は長さ M の英小文字からなる文字列ですが、通信中のノイズにより一部の位置が読み取れなくなっており、さらに読み取れた位置にも誤りが含まれている可能性があります。

一方、これらの暗号文の元になったはずの長さ M の原文が N 個与えられています。暗号化の際、すべての原文は共通のシフト量で巡回シフトされて暗号文になったと考えられていますが、正確なシフト量も、どの暗号文がどの原文に対応するかも分かっていません。

高橋君は、不一致数の合計が最小になるようなシフト量と対応を見つけたいと考えています。

用語の定義

文字列の各位置は 1 から M までの整数で表します。

巡回シフトと位置の対応: シフト量 r(0 \leq r < M の整数)で暗号文と原文を比較するとは、原文を左に r 文字だけ巡回シフトした文字列と暗号文を、同じ位置同士で比較することです。具体的には、暗号文の位置 p に対応する原文の位置は ((p + r - 1) \bmod M) + 1 です。

ノイズ情報: 各暗号文 i(1 \leq i \leq N)には、受信した文字列 S_i と、読み取り可否を表す文字列 C_i が対になっています。C_i の位置 p が 0 ならばその位置の文字は読み取れており、比較の対象とします。1 ならばその位置は読み取れなかったことを意味し、比較の対象外とします。

不一致数: 暗号文 i と原文 j をシフト量 r で対応させたときの不一致数を次のように定義します。C_i の位置 p が 0 であるすべての位置 p(1 \leq p \leq M)について、S_i の位置 p の文字と T_j の位置 ((p + r - 1) \bmod M) + 1 の文字を比較し、異なるものの個数を不一致数とします。

求めるもの

シフト量 r(0 \leq r < M)を1つ選び、N 通の暗号文と N 個の原文の間の1対1対応(すなわち、暗号文 1, 2, \ldots, N に原文を重複なく1つずつ割り当てる方法)を1つ定めます。シフト量 r はすべての対応ペアで共通です。

このとき、すべての対応ペアにおける不一致数の合計を考え、その最小値を求めてください。

変更クエリ

暗号文や原文に対して Q 回の変更が順に行われます。各変更を行った後の状態について、上記の最小値を求めてください。

制約

  • 1 \leq N \leq 5
  • 1 \leq M \leq 500
  • 1 \leq Q \leq 500
  • M Q \leq 5 \times 10^4
  • S_i は長さ M の英小文字からなる文字列である(1 \leq i \leq N)
  • C_i は長さ M の 0 と 1 からなる文字列である(1 \leq i \leq N)
  • T_j は長さ M の英小文字からなる文字列である(1 \leq j \leq N)
  • 各変更クエリは以下の3種類のいずれかである
  • 形式 1 i p x:1 \leq i \leq N、1 \leq p \leq M、x は英小文字
  • 形式 2 i p b:1 \leq i \leq N、1 \leq p \leq M、b は 0 または 1
  • 形式 3 j p x:1 \leq j \leq N、1 \leq p \leq M、x は英小文字

入力

N M Q
S_1
C_1
S_2
C_2
\vdots
S_N
C_N
T_1
T_2
\vdots
T_N
query_1
query_2
\vdots
query_Q

入力は以下の構成で与えられます。

  • 1 行目には、暗号文と原文の個数 N、各文字列の長さ M、変更の回数 Q が空白区切りで与えられる。
  • 続く 2N 行で、N 通の暗号文の初期状態が与えられる。暗号文 i(1 \leq i \leq N)について、受信文字列 S_i とノイズ情報 C_i がこの順に1行ずつ与えられる。
  • 続く N 行で、N 個の原文の初期状態が与えられる。j 番目(1 \leq j \leq N)の行に原文 T_j が与えられる。
  • 続く Q 行で、変更クエリが1行ずつ与えられる。各変更は次のいずれかの形式である。
  • 1 i p x:暗号文 i の受信文字列 S_i の位置 p の文字を x に変更する。
  • 2 i p b:暗号文 i のノイズ情報 C_i の位置 p の値を b に変更する。
  • 3 j p x:原文 T_j の位置 p の文字を x に変更する。

出力

Q 行出力せよ。k 行目(1 \leq k \leq Q)には、k 回目の変更を行った後の状態における、不一致数の合計の最小値を整数で出力せよ。


入力例 1

2 4 5
abca
0000
zzzz
1010
bcaa
zzzz
1 1 2 c
2 2 1 0
3 1 4 b
2 1 3 1
1 2 4 a

出力例 1

1
1
2
2
3

入力例 2

1 5 4
abcde
00101
cdeab
3 1 1 a
2 1 3 0
1 1 5 b
3 1 4 z

出力例 2

0
1
1
2

入力例 3

3 8 10
abcdefgh
00001111
hgfedcba
01010101
aaaabbbb
11110000
cdefghab
fedcbahg
bbbbaaaa
1 1 4 z
2 3 2 0
3 2 8 a
1 2 1 f
2 1 5 0
3 1 3 x
2 2 4 0
1 3 8 c
3 3 5 z
2 1 1 1

出力例 3

3
3
3
4
4
5
5
5
5
5

入力例 4

5 20 15
abcdefghijklmnopqrst
00000000000000000000
zyxwvutsrqponmlkjihg
01010101010101010101
aaaaaaaaaabbbbbbbbbb
11111000001111100000
qwertyuiopasdfghjklz
00110011001100110011
mnopqrstuvwxyzabcdef
10000000000000000001
defghijklmnopqrstabc
ghijklmnopqrstuvwxyz
bbbbbbbbbbaaaaaaaaaa
poiuytrewqlkjhgfdsaz
nopqrstuvwxyzabcdefg
1 1 20 a
2 2 2 0
3 3 10 c
1 4 5 z
2 5 1 0
3 1 1 q
1 3 11 a
2 4 19 1
3 5 20 y
1 2 7 b
2 1 10 1
3 4 4 a
1 5 14 m
2 3 6 1
3 2 15 d

出力例 4

41
42
42
42
42
42
42
42
42
42
41
41
42
42
43

入力例 5

1 1 5
a
0
b
3 1 1 a
1 1 1 z
2 1 1 1
3 1 1 z
2 1 1 0

出力例 5

0
1
0
0
0

Score : 400 pts

Problem Statement

Takahashi has received N encrypted messages. Each encrypted message is a string of length M consisting of lowercase English letters, but due to noise during transmission, some positions have become unreadable, and even the readable positions may contain errors.

Meanwhile, N original plaintext strings of length M are given, which are supposed to be the sources of these encrypted messages. It is believed that during encryption, all plaintexts were cyclically shifted by a common shift amount to produce the encrypted messages, but neither the exact shift amount nor the correspondence between encrypted messages and plaintexts is known.

Takahashi wants to find the shift amount and correspondence that minimize the total number of mismatches.

Definitions

Each position in a string is represented by an integer from 1 to M.

Cyclic shift and position correspondence: Comparing an encrypted message with a plaintext using shift amount r (an integer with 0 \leq r < M) means comparing the encrypted message with the string obtained by cyclically shifting the plaintext r characters to the left, at corresponding positions. Specifically, the plaintext position corresponding to position p in the encrypted message is ((p + r - 1) \bmod M) + 1.

Noise information: Each encrypted message i (1 \leq i \leq N) is paired with a received string S_i and a readability string C_i. If position p of C_i is 0, then the character at that position is readable and is subject to comparison. If it is 1, it means that position was unreadable and is excluded from comparison.

Number of mismatches: The number of mismatches when pairing encrypted message i with plaintext j using shift amount r is defined as follows. For all positions p (1 \leq p \leq M) where position p of C_i is 0, compare the character at position p of S_i with the character at position ((p + r - 1) \bmod M) + 1 of T_j. The number of positions where they differ is the number of mismatches.

Objective

Choose one shift amount r (0 \leq r < M), and determine a one-to-one correspondence between the N encrypted messages and the N plaintexts (i.e., assign one plaintext to each of encrypted messages 1, 2, \ldots, N without repetition). The shift amount r is common to all correspondence pairs.

Consider the total number of mismatches across all correspondence pairs, and find its minimum value.

Update Queries

Q updates are applied sequentially to the encrypted messages or plaintexts. After each update, find the above minimum value for the current state.

Constraints

  • 1 \leq N \leq 5
  • 1 \leq M \leq 500
  • 1 \leq Q \leq 500
  • M Q \leq 5 \times 10^4
  • S_i is a string of length M consisting of lowercase English letters (1 \leq i \leq N)
  • C_i is a string of length M consisting of 0 and 1 (1 \leq i \leq N)
  • T_j is a string of length M consisting of lowercase English letters (1 \leq j \leq N)
  • Each update query is one of the following three types:
  • Format 1 i p x: 1 \leq i \leq N, 1 \leq p \leq M, x is a lowercase English letter
  • Format 2 i p b: 1 \leq i \leq N, 1 \leq p \leq M, b is 0 or 1
  • Format 3 j p x: 1 \leq j \leq N, 1 \leq p \leq M, x is a lowercase English letter

Input

N M Q
S_1
C_1
S_2
C_2
\vdots
S_N
C_N
T_1
T_2
\vdots
T_N
query_1
query_2
\vdots
query_Q

The input is given in the following format:

  • The first line contains the number of encrypted messages and plaintexts N, the length of each string M, and the number of updates Q, separated by spaces.
  • The following 2N lines give the initial state of the N encrypted messages. For encrypted message i (1 \leq i \leq N), the received string S_i and noise information C_i are given in this order, one per line.
  • The following N lines give the initial state of the N plaintexts. The j-th line (1 \leq j \leq N) contains plaintext T_j.
  • The following Q lines give the update queries, one per line. Each update is in one of the following formats:
  • 1 i p x: Change the character at position p of the received string S_i of encrypted message i to x.
  • 2 i p b: Change the value at position p of the noise information C_i of encrypted message i to b.
  • 3 j p x: Change the character at position p of plaintext T_j to x.

Output

Output Q lines. The k-th line (1 \leq k \leq Q) should contain the minimum value of the total number of mismatches after the k-th update has been applied, as an integer.


Sample Input 1

2 4 5
abca
0000
zzzz
1010
bcaa
zzzz
1 1 2 c
2 2 1 0
3 1 4 b
2 1 3 1
1 2 4 a

Sample Output 1

1
1
2
2
3

Sample Input 2

1 5 4
abcde
00101
cdeab
3 1 1 a
2 1 3 0
1 1 5 b
3 1 4 z

Sample Output 2

0
1
1
2

Sample Input 3

3 8 10
abcdefgh
00001111
hgfedcba
01010101
aaaabbbb
11110000
cdefghab
fedcbahg
bbbbaaaa
1 1 4 z
2 3 2 0
3 2 8 a
1 2 1 f
2 1 5 0
3 1 3 x
2 2 4 0
1 3 8 c
3 3 5 z
2 1 1 1

Sample Output 3

3
3
3
4
4
5
5
5
5
5

Sample Input 4

5 20 15
abcdefghijklmnopqrst
00000000000000000000
zyxwvutsrqponmlkjihg
01010101010101010101
aaaaaaaaaabbbbbbbbbb
11111000001111100000
qwertyuiopasdfghjklz
00110011001100110011
mnopqrstuvwxyzabcdef
10000000000000000001
defghijklmnopqrstabc
ghijklmnopqrstuvwxyz
bbbbbbbbbbaaaaaaaaaa
poiuytrewqlkjhgfdsaz
nopqrstuvwxyzabcdefg
1 1 20 a
2 2 2 0
3 3 10 c
1 4 5 z
2 5 1 0
3 1 1 q
1 3 11 a
2 4 19 1
3 5 20 y
1 2 7 b
2 1 10 1
3 4 4 a
1 5 14 m
2 3 6 1
3 2 15 d

Sample Output 4

41
42
42
42
42
42
42
42
42
42
41
41
42
42
43

Sample Input 5

1 1 5
a
0
b
3 1 1 a
1 1 1 z
2 1 1 1
3 1 1 z
2 1 1 0

Sample Output 5

0
1
0
0
0