#P5821. Mismatch Degree Computation

    ID: 19049 Type: Default 1000ms 256MiB

Mismatch Degree Computation

Mismatch Degree Computation

You are given a secret password string T and an initial guess string P, both consisting only of lowercase letters. The length of T can be up to 200,000, while the length of P is l (with l ≤ |T|). The goal is to compute the mismatch degree between P and the substring of T that is of the same length as P (i.e. the first l characters of T).

The mismatch degree between two strings s and t of equal length is defined as:

\[ \text{Mismatch}(s, t) = \sum_{i=1}^{|s|} \Big(\text{val}(s_i) - \text{val}(t_i)\Big)^2 \]

where the function val is defined as follows:

  • \(\text{val}(a)=1\),
  • \(\text{val}(b)=2\),
  • ... ,
  • \(\text{val}(z)=26\).

After the initial guess, you are given Q queries. Each query specifies an update to the guess string P: change the character at a given position to a new character. After processing the initial input and after each query update, output the current mismatch degree between P and the first l characters of T.

Note: Positions are 1-indexed.

inputFormat

The input consists of the following lines:

  1. The first line contains the string T (the secret password).
  2. The second line contains the string P (the initial guess string).
  3. The third line contains an integer Q, the number of queries.
  4. Each of the following Q lines contains an integer pos and a character c, meaning that the character at position pos of P should be changed to c.

It is guaranteed that 1 ≤ pos ≤ |P|.

outputFormat

Output Q+1 lines:

  1. The first line should contain the mismatch degree between P and the first |P| characters of T in the initial state.
  2. Each of the subsequent Q lines should contain the updated mismatch degree after processing each query in order.

sample

abcde
abcde
2
3 z
5 a
0

529 545

</p>