CF1252D.Find String in a Grid

普及/提高-

通过率:0%

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:

  • 1rR1 \le r \le R and rr+ΔrRr \le r + \Delta r \le R
  • 1cC1 \le c \le C and cc+ΔcCc \le c + \Delta c \le C
  • S=Gr,cGr,c+1Gr,c+ΔcGr+1,c+ΔcGr+Δ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 ( 1R,C5001 \le R, C \le 500 ; 1Q2000001 \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 200000200\,000 . The sum of the length of all QQ strings combined is not more than 200000200\,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 .
首页