problems / 132

Circular Buffer Alignment

medium strings string-matching 1000 ms · 256 MB

Circular Buffer Alignment

A radio module keeps its last n received bytes in a ring buffer of n slots, numbered 0 to n - 1. After a crash, a debugger dumped the buffer slot by slot, starting at slot 0, into a string A. The write pointer was lost, so nobody knows which slot holds the first byte of the message.

The message the module should have received is known: the string B, of the same length n. Reading the buffer from slot k means reading slots k, k + 1, …, n - 1, 0, 1, …, k - 1 in that order. The offset k is consistent if this reading is exactly B.

Find every consistent offset. A buffer with repeating content can have many of them.

Input

The first line contains n (1 ≤ n ≤ 10^6), the number of slots.

The second line contains the dump A, a string of exactly n lowercase English letters.

The third line contains the expected message B, a string of exactly n lowercase English letters.

Output

On the first line print c, the number of consistent offsets.

On the second line print the c consistent offsets in increasing order, separated by single spaces. If c = 0, print an empty line.

Notes

In the first example, reading lohel from slot 2 gives hel followed by lo, which is hello. No other offset works.

In the second example, the dump repeats abc, so reading from slots 1, 4 and 7 all give bcabcabca.

Example 1

Input

5
lohel
hello

Output

1
2

Example 2

Input

9
abcabcabc
bcabcabca

Output

3
1 4 7

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

Hint 1

Reading the buffer from slot k gives a window of length n in a doubled copy of the dump. What does that turn the question into?

Hint 2

Find every occurrence of B in the doubled dump with a linear-time string matching method such as KMP, keeping only starts below n. Comparing each rotation directly can be quadratic on periodic input.

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

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