#abc324e. E - Joint Two Strings

E - Joint Two Strings

Score : 500500 points

问题描述

你将得到 NN 个由小写英文字母组成的字符串 S1,S2,,SNS_1, S_2, \ldots, S_N,以及一个由小写英文字母组成的字符串 TT

存在 N2N^2 对整数 (i,j)(i, j),满足 1i,jN1 \leq i, j \leq N。请输出在这些对中满足以下条件的对的数量。

  • SiS_iSjS_j 按照顺序拼接后,其中包含 TT 作为(不一定连续的)子序列。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given NN strings S1,S2,,SNS_1, S_2, \ldots, S_N consisting of lowercase English letters, and a string TT consisting of lowercase English letters.

There are N2N^2 pairs (i,j)(i, j) of integers between 11 and NN, inclusive. Print the number of pairs among them that satisfy the following condition.

  • The concatenation of SiS_i and SjS_j in this order contains TT as a (not necessarily contiguous) subsequence.

Constraints

  • NN is an integer.
  • 1N5×1051 \leq N \leq 5 \times 10^5
  • SiS_i and TT are strings of length 11 to 5×1055 \times 10^5, inclusive, consisting of lowercase English letters.
  • The total length of S1,S2,,SNS_1, S_2, \ldots, S_N is at most 5×1055 \times 10^5.

Input

The input is given from Standard Input in the following format:

NN TT

S1S_1

S2S_2

\vdots

SNS_N

Output

Print the answer.

Sample Input 1

3 bac
abba
bcb
aaca

Sample Output 1

3

The pairs (i,j)(i, j) that satisfy the condition in the problem statement are (1,2),(1,3),(2,3)(1, 2), (1, 3), (2, 3), as seen below.

  • For (i,j)=(1,2)(i, j) = (1, 2), the concatenation abbabcb of S1S_1 and S2S_2 in this order contains bac as a subsequence.
  • For (i,j)=(1,3)(i, j) = (1, 3), the concatenation abbaaaca of S1S_1 and S3S_3 in this order contains bac as a subsequence.
  • For (i,j)=(2,3)(i, j) = (2, 3), the concatenation bcbaaca of S2S_2 and S3S_3 in this order contains bac as a subsequence.

Sample Input 2

5 xx
x
x
x
x
x

Sample Output 2

25

Sample Input 3

1 y
x

Sample Output 3

0

Sample Input 4

10 ms
mkgn
m
hlms
vmsle
mxsm
nnzdhi
umsavxlb
ffnsybomr
yvmm
naouel

Sample Output 4

68

update @ 2024/3/10 01:46:28