아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Shuffle

면접 대비

시간 제한2초메모리 제한512 MB

요약
길이가 같고 짝수인 두 문자열 s와 t가 주어질 때, 홀수 위치 문자를 앞으로 모으는 shuffle 연산을 최소 몇 번 적용해야 t가 되는지 구하고, 불가능하면 -1을 출력한다.
난이도

보통10점 중 6점

유형
문자열, 시뮬레이션, 수학, 구현
정답자
아직 제출이 없습니다

문제

Given a string of even length s=s_1…s_ns = s\_1 \ldots s\_n, we define shuffle\mathrm{shuffle} operation which transforms a string into a new string according to the following rule:

shuffle(s)=s_1s_3…s_n−1s_2s_4…s_n\mathrm{shuffle}(s) = s\_1 s\_3 \ldots s\_{n-1} s\_2 s\_4 \ldots s\_n

For example, shuffle(abcdef)=acebdf\mathrm{shuffle}(\texttt{abcdef}) = \texttt{acebdf}.

You are given two strings of equal even length, ss and tt. How many times do you have to apply shuffle operation to ss in order to get tt as a result?

In the other words, find minimum kk such that shuffle(shuffle(…shuffle⏟_k times(s)… ))=t\underbrace{\mathrm{shuffle}(\mathrm{shuffle}(\dots \mathrm{shuffle}}\_{k\ \mathrm{times}} (s)\dots)) = t or report that it is not possible to reach tt in any number of operations.

입력

The first line of input contains a string ss, the second contains a string tt (∣s∣=∣t∣|s| = |t|, 2≤∣s∣≤1062 \leq |s| \leq 10^6, ∣s∣|s| is even). Both strings consist of lowercase English characters.

출력

Print minimum non-negative kk such that it is possible to obtain tt from ss by applying shuffle operation kk times (or maybe not applying at all if k=0k = 0), or print -1 if it is impossible.

예제3

  1. 예제 1

    입력
    abcdef
    aedcbf
    
    예상 출력
    2
    
  2. 예제 2

    입력
    petrozavodsk
    poztsvoedark
    
    예상 출력
    3
    
  3. 예제 3

    입력
    qwerty
    ytrewq
    
    예상 출력
    -1