#P10634. Melodic Cipher Matching

    ID: 12660 Type: Default 1000ms 256MiB

Melodic Cipher Matching

Melodic Cipher Matching

During a recent war between two countries, A and B, the military commander of country A, Dick, faced a serious intelligence challenge. Country B hired a cipher expert, Easy, who encrypts all communications as melodies. Easy converts a signal such as \(11556654433221\) into an equal‐length sequence (for example, \(1,1,5,5,6,6,\dots\)) and then applies a transformation to obtain a new melody. The transformation has the following properties: if two pitches in the original melody are equal, then their corresponding pitches in the transformed melody are equal; if one pitch is less than another, then its transformed pitch is also less than the other; and similarly for greater than. For instance, the melodies 11221 and 55775 may represent the same tune.

Dick has intercepted a long encrypted signal and he also knows the meaning of some specific melodies. He now wonders whether a given known melody appears in the intercepted signal. If it appears, he wants to know the number of its occurrences and the starting positions of each occurrence.

Note: Two sequences are considered to represent the same melody if they are order isomorphic. Formally, let \(A=a_1,a_2,\dots,a_m\) be the known melody and let \(B=b_1,b_2,\dots,b_m\) be a contiguous subsequence from the intercepted signal. They represent the same melody if and only if for any indices \(i,j\):

  • if \(a_i = a_j\) then \(b_i = b_j\);
  • if \(a_i < a_j\) then \(b_i < b_j\);
  • if \(a_i > a_j\) then \(b_i > b_j\).

Your task is to detect all occurrences of the known melody in the intercepted signal.

inputFormat

The input consists of two lines:

  1. The first line contains the intercepted melody, which is a non-empty string of digits (without spaces).
  2. The second line contains the known melody (also a string of digits) whose pattern is to be searched for.

outputFormat

If the known melody appears in the intercepted signal, output two lines:

  1. The first line contains the total count of occurrences.
  2. The second line contains the 1-indexed starting positions (in increasing order) of each occurrence, separated by a single space.

If there is no occurrence, output a single line containing 0.

sample

11221
11221
1

1

</p>