CF1252D.Find String in a Grid

NOI/NOI+/CTSC

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You have a grid GG containing RR rows (numbered from 11 to RR, top to bottom) and CC columns (numbered from 11 to CC, left to right) of uppercase characters. The character in the rthr^{th} row and the cthc^{th} column is denoted by Gr,cG_{r, c}. You also have QQ strings containing uppercase characters. For each of the string, you want to find the number of occurrences of the string in the grid.

An occurrence of string SS in the grid is counted if SS can be constructed by starting at one of the cells in the grid, going right 00 or more times, and then going down 00 or more times. Two occurrences are different if the set of cells used to construct the string is different. Formally, for each string SS, you would like to count the number of tuples ⟨r,c,Δr,Δc⟩\langle r, c, \Delta r, \Delta c \rangle such that:

  • 1≤r≤R1 \le r \le R and r≤r+Δr≤Rr \le r + \Delta r \le R
  • 1≤c≤C1 \le c \le C and c≤c+Δc≤Cc \le c + \Delta c \le C
  • S=Gr,cGr,c+1…Gr,c+ΔcGr+1,c+Δc…Gr+Δr,c+ΔcS = G_{r, c} G_{r, c + 1} \dots G_{r, c + \Delta c} G_{r + 1, c + \Delta c} \dots G_{r + \Delta r, c + \Delta c}

你有一个包含 RR 行(从上到下编号为 11 至 RR)和 CC 列(从左到右编号为 11 至 CC)的大写字母网格 GG。第 rr 行第 cc 列的字符记为 Gr,cG_{r, c}。此外,你还有 QQ 个由大写字母组成的字符串。对于每个字符串,你需要找出它在该网格中出现的次数。

字符串 SS 在网格中的一次出现定义为:从网格中的某个单元格出发,向右移动 00 次或多次,然后向下移动 00 次或多次,从而拼出字符串 SS。若构造字符串所使用的单元格集合不同,则视为两次不同的出现。形式化地,对每个字符串 SS,你需要统计满足以下条件的四元组 ⟨r,c,Δr,Δc⟩\langle r, c, \Delta r, \Delta c \rangle 的数量:

  • 1≤r≤R1 \le r \le R 且 r≤r+Δr≤Rr \le r + \Delta r \le R
  • 1≤c≤C1 \le c \le C 且 c≤c+Δc≤Cc \le c + \Delta c \le C
  • S=Gr,cGr,c+1…Gr,c+ΔcGr+1,c+Δc…Gr+Δr,c+ΔcS = G_{r, c} G_{r, c + 1} \dots G_{r, c + \Delta c} G_{r + 1, c + \Delta c} \dots G_{r + \Delta r, c + \Delta c}

输入格式

Input begins with a line containing three integers: RR CC QQ (1≤R,C≤5001 \le R, C \le 500; 1≤Q≤200 0001 \le Q \le 200\,000) representing the size of the grid and the number of strings, respectively. The next RR lines each contains CC uppercase characters representing the grid. The cthc^{th} character on the rthr^{th} line is Gr,cG_{r, c}. The next QQ lines each contains a string SS containing uppercase characters. The length of this string is a positive integer not more than 200 000200\,000. The sum of the length of all QQ strings combined is not more than 200 000200\,000.

输入的第一行包含三个整数:RR、CC 和 QQ(1≤R,C≤5001 \le R, C \le 500;1≤Q≤200 0001 \le Q \le 200\,000),分别表示网格的尺寸以及字符串的数量。接下来的 RR 行,每行包含 CC 个大写字符,表示该网格。第 rr 行的第 cc 个字符为 Gr,cG_{r, c}。随后的 QQ 行,每行包含一个由大写字符组成的字符串 SS。该字符串的长度为正整数,且不超过 200 000200\,000。所有 QQ 个字符串的总长度不超过 200 000200\,000。

输出格式

For each query in the same order as input, output in a line an integer representing the number of occurrences of the string in the grid.

对于每个查询,按照输入中的相同顺序,在一行中输出一个整数,表示该字符串在网格中出现的次数。

输入输出样例

  • 输入#1

    3 3 5
    ABC
    BCD
    DAB
    ABC
    BC
    BD
    AC
    A

    输出#1

    2
    3
    1
    0
    2
  • 输入#2

    2 3 3
    AAA
    AAA
    A
    AAA
    AAAAA

    输出#2

    6
    4
    0

说明/提示

Explanation for the sample input/output #1

  • There are 22 occurrences of "ABC", represented by the tuples ⟨1,1,1,1⟩\langle 1, 1, 1, 1 \rangle and ⟨1,1,0,2⟩\langle 1, 1, 0, 2 \rangle.
  • There are 33 occurrences of "BC", represented by the tuples ⟨1,2,0,1⟩\langle 1, 2, 0, 1 \rangle, ⟨1,2,1,0⟩\langle 1, 2, 1, 0 \rangle, and ⟨2,1,0,1⟩\langle 2, 1, 0, 1 \rangle.
  • There is 11 occurrence of "BD", represented by the tuple ⟨2,1,1,0⟩\langle 2, 1, 1, 0 \rangle.
  • There is no occurrence of "AC".
  • There are 22 occurrences of "A", represented by the tuples ⟨1,1,0,0⟩\langle 1, 1, 0, 0 \rangle and ⟨3,2,0,0⟩\langle 3, 2, 0, 0 \rangle.

样例输入/输出 #1 的说明

  • “ABC” 出现了 22 次,对应的四元组为 ⟨1,1,1,1⟩\langle 1, 1, 1, 1 \rangle 和 ⟨1,1,0,2⟩\langle 1, 1, 0, 2 \rangle。
  • “BC” 出现了 33 次,对应的四元组为 ⟨1,2,0,1⟩\langle 1, 2, 0, 1 \rangle、⟨1,2,1,0⟩\langle 1, 2, 1, 0 \rangle 和 ⟨2,1,0,1⟩\langle 2, 1, 0, 1 \rangle。
  • “BD” 出现了 11 次,对应的四元组为 ⟨2,1,1,0⟩\langle 2, 1, 1, 0 \rangle。
  • “AC” 未出现。
  • “A” 出现了 22 次,对应的四元组为 ⟨1,1,0,0⟩\langle 1, 1, 0, 0 \rangle 和 ⟨3,2,0,0⟩\langle 3, 2, 0, 0 \rangle。

输入解题思路,AI测评打分。不知道怎么写?

首页