Wildcard Pattern Starts
Alice is receiving a long string transmission over a noisy channel. She believes the transmission contains one or more copies of a secret message, but parts of it may be corrupted by interference.
She knows the message pattern structure:
- Some characters are known with certainty (fixed letters).
?represents exactly one corrupted character (unknown, but exactly one character).*represents a run of zero or more corrupted characters (any number of any characters, including none).
Given the received transmission text and the message pattern pattern, find all starting positions (0-indexed) in the text where the pattern could match.
A match means that starting from that position, reading characters from the text consumes exactly the characters matched by the pattern. Each * can match a different length at different positions, and the matched substring must be contiguous.
Input
The first line contains an integer n (1 ≤ n ≤ 10^5), the length of the transmission text.
The second line contains the text: a string of n lowercase English letters.
The third line contains an integer m (1 ≤ m ≤ 10^5), the length of the pattern.
The fourth line contains the pattern: a string of length m containing lowercase letters, ?, and *.
Output
Print the starting positions (0-indexed) where the pattern matches the text, in ascending order, separated by spaces. Positions range from 0 to n inclusive: a pattern made only of * matches the empty string at every position, including n. If no positions match, print nothing (empty output).
Constraints
- 1 ≤ n ≤ 10^5
- 1 ≤ m ≤ 10^5
- The text contains only lowercase English letters.
- The pattern contains only lowercase English letters,
?, and*.
Notes
- The pattern can have multiple
*and?in any arrangement. - A
*can match zero or more characters. - A
?must match exactly one character. - The entire pattern must match a contiguous substring starting at the reported position; that substring may end anywhere in the text (nothing has to match after the last block).
- Examples: with text "abcabc" and pattern "a?c", the matches are at positions 0 and 3. With text "aaa" and pattern "aa", the matches are at positions 0 and 1 (matching "aa" and "aa" respectively, with "" consuming one or zero characters).