#P5410. 【模板】扩展 KMP / exKMP(Z 函数)

【模板】扩展 KMP / exKMP(Z 函数)

Problem Description

Given two strings a,ba, b, you need to find two arrays:

  • The zz function array zz of bb, that is, the LCP length between bb and each suffix of bb.
  • The LCP length array pp between bb and each suffix of aa.

For an array aa of length nn, define its weight as xori=1ni×(ai+1)\operatorname{xor}_{i=1}^n i \times (a_i + 1).

Input Format

Two lines, containing two strings a,ba, b.

Output Format

The first line contains one integer, the weight of zz.

The second line contains one integer, the weight of pp.

aaaabaa
aaaaa
6
21

Hint

Sample explanation:

z={5 4 3 2 1}z = \{5\ 4\ 3\ 2\ 1\}, p={4 3 2 1 0 2 1}p = \{4\ 3\ 2\ 1\ 0\ 2\ 1\}.


Constraints:

For the first test point, a,b2×103|a|, |b| \le 2 \times 10^3.

For the second test point, a,b2×105|a|, |b| \le 2 \times 10^5.

For 100%100\% of the data, 1a,b2×1071 \le |a|, |b| \le 2 \times 10^7, and all characters are lowercase letters.

Translated by ChatGPT 5