#K35337. Count Key Phrase Occurrences

    ID: 25509 Type: Default 1000ms 256MiB

Count Key Phrase Occurrences

Count Key Phrase Occurrences

Given a text document consisting of n lines and a key phrase composed of m words, your task is to determine the total number of times the key phrase appears in the document. The text document is processed by concatenating all lines into a single string with a single space between each line.

In mathematical terms, if we denote the concatenated text string as S and the key phrase as P, you are to compute the number of non-overlapping occurrences, i.e., $$\text{Answer} = \#\{ i : S[i:i+|P|]=P \}\,.$$

Note that the key phrase must match exactly (including spaces) as it appears in the combined text.

inputFormat

The input is read from stdin and follows the format below:

n m
line1
line2
... (n lines total)
key_phrase

Where:

  • n: An integer representing the number of lines in the text document.
  • m: An integer representing the number of words in the key phrase (this value is provided for informational purposes).
  • line1, line2, ..., line n: The lines of the text document.
  • key_phrase: The key phrase to search for in the concatenated text.
  • </p>

    outputFormat

    The output is a single integer printed to stdout representing the total number of non-overlapping occurrences of the key phrase in the text document.

    ## sample
    3 2
    hello world
    this is a test
    hello world again
    hello world
    2