#K42742. Pattern Search in a Text

    ID: 27155 Type: Default 1000ms 256MiB

Pattern Search in a Text

Pattern Search in a Text

You are given two strings: a pattern and a text. Your task is to find all occurrences of the pattern in the text using the Knuth-Morris-Pratt (KMP) algorithm. More formally, let \(M\) be the length of the pattern and \(N\) be the length of the text. You need to output the starting indices of every substring in the text that exactly matches the pattern. If there is no occurrence of the pattern in the text, output an empty line.

Example:

Input:
abc
ababcabc

Output: 2 5

</p>

The indices are 0-indexed. Ensure your solution reads input from standard input (stdin) and writes the result to standard output (stdout).

inputFormat

The input consists of two lines. The first line contains the pattern string, and the second line contains the text string.

pattern
text

outputFormat

Print a single line containing the starting indices (0-indexed) of each occurrence of the pattern in the text, separated by a space. If the pattern is not found, print an empty line.

## sample
abc
ababcabc
2 5