problems / 197

Wildcard Pattern Starts

hard strings string-matching bit-manipulation greedy 1000 ms · 256 MB

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).

Example 1

Input

6
abcabc
3
a?c

Output

0 3

Example 2

Input

3
aaa
3
a*a

Output

0 1

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

Hint 1

Split the pattern at its '*' characters into blocks of letters and '?'. Only the first block is pinned to the start; every later block just has to appear, in order and without overlapping, somewhere after it, and the match may end anywhere.

Hint 2

Checking each start separately repeats the same search for the later blocks. If the blocks after the first one can be placed when they begin after position p, can they also be placed when they begin earlier? What single number would then decide every start at once?

Hint 3

You need the occurrences of blocks that contain '?', which rules out plain KMP or hashing. Keep, for each letter, a bitset of where it appears in the text, and think about how shifts and ANDs can test all positions at once.

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

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