#P10069. [CCO 2023] Flip it and Stick it

[CCO 2023] Flip it and Stick it

Problem Description

Finn is playing a game called “Flip it and Stick it”, abbreviated as FiSi. FiSi is a single-player game played on two strings SS and TT consisting of 0s and 1s. Finn can perform the following type of operation:

Choose a substring of SS and reverse it, then stick the parts of the string back together in the original order to form a new string SS. For example, Finn can take the string S=101100S=101100, choose the substring 011011 starting from position 2 (the string is indexed from 11), and create the string S=111000S=111000 in one operation.

If SS does not contain TT as a substring, Finn wins the game. Your task is to help Finn determine the length of the shortest sequence of operations needed to win the game, or tell him that the game cannot be won.

Input Format

The first line contains a string SS.

The second line contains a string TT.

Output Format

Output one line containing an integer: the minimum number of operations needed to win if it is possible, otherwise output 1-1.

100110
10
2
000
00
-1

Hint

Sample Explanation 1

Finn starts from the string 100110100110. He cannot make 1010 not be a substring with one operation, but he can do it in two operations.

For example, his first operation can be to reverse the substring from position 4 to position 6 (110110), obtaining 100011100011. Then, his second operation can be to reverse the substring from position 1 to position 4 (10001000), obtaining 000111000111, which does not contain 1010 as a substring.

Sample Explanation 2

No matter how many operations Finn performs, the string TT will always be a substring of SS.

For all testdata, 1S2×1051 \leq|S| \leq 2\times 10^5, 1T31 \leq|T| \leq 3.

Let T=l|T|=l.

Subtask ID Score Constraint on ll
1 4 l=1l=1
2 12 l=2,T1T2l=2, T_{1} \neq T_{2}
3 16 l=2l=2
4 20 l=3,T1T3l=3, T_{1} \neq T_{3}
5 l=3,T1T2l=3, T_{1} \neq T_{2}
6 28 l=3l=3

Translated by ChatGPT 5