All Occurrences of a Pattern

medium strings string-matching 2000 ms · 256 MB

All Occurrences of a Pattern

You are given a text s and a pattern p, both strings of lowercase English letters.

Find every starting position in s where p occurs. Occurrences may overlap: if p starts at position i, the next occurrence may start anywhere at or after position i + 1, not only after the previous occurrence ends.

Input

The first line contains the text s (1 ≤ |s| ≤ 10^6).

The second line contains the pattern p (1 ≤ |p| ≤ 10^6).

Both strings consist only of lowercase English letters.

Output

Print a single integer k, the number of occurrences of p in s.

On the next line print the k starting positions where p occurs in s, in increasing order, 1-based, separated by single spaces. If k = 0, print an empty line.

Notes

In the first example, p = "ab" occurs in s = "ababab" starting at positions 1, 3 and 5 — every occurrence of p that fits, including ones that share characters with the previous one.

In the second example, p = "aa" occurs in s = "aaaa" at positions 1, 2 and 3: the occurrences starting at 1 and 2 overlap in the shared middle a.

Example 1

Input

ababab
ab

Output

3
1 3 5

Example 2

Input

aaaa
aa

Output

3
1 2 3

Hints · reveal one at a time if you're stuck

Hint 1

Trying the pattern at every starting position is too slow when both strings are long, because a mismatch can be found only after many comparisons. Think about what you already learn about the text from a partial match.

Hint 2

Use the prefix function of the pattern (as in KMP), run over the text. Put a separator between the pattern and the text when you combine them, so the match length can never overflow past the pattern, and then report each position where the full pattern length is reached, converted to a 1-based start.

report an issue Prefer your own editor? sooks prep 99 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.