Scary Subsequences

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

요약
세 고정 문자열 x, y, z와 이들을 모두 부분열로 포함하는 더 긴 문자열 s가 주어질 때, x, y, z 모두의 부분열이 아닌 s의 가장 짧은 부분열의 길이를 구한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 문자열, 그리디
정답자
아직 제출이 없습니다

문제

In this problem, all strings consist only of four characters: a, b, c, and d.

Busy Beaver has three strings xx, yy, and zz that he's really scared of. In particular, he only likes strings that are not a subsequence of any of them.

Answer QQ queries. In query ii, you are given a string s_is\_i, such that xx, yy, and zz are all subsequences of s_is\_i and ∣s_i∣>max⁡(∣x∣,∣y∣,∣z∣)|s\_i| > \max(|x|, |y|, |z|). Help Busy Beaver find the length of the shortest subsequence of s_is\_i that is not a subsequence of any of xx, yy, or zz.


1A string ss is a subsequence of a string tt if ss can be obtained from tt by deleting some (possibly none or all) characters from tt, without reordering the remaining characters.

입력

The first three lines of the input contain the strings xx, yy, and zz (1≤∣x∣,∣y∣,∣z∣≤601 \le |x|, |y|, |z| \le 60), consisting of characters a, b, c, d.

The next line contains a single positive integer QQ (1≤Q≤1.5⋅1051 \le Q \le 1.5 \cdot 10^5).

The next QQ lines each contain a string, the ii-th of which is s_is\_i (max⁡(∣x∣,∣y∣,∣z∣)<∣s_i∣≤3⋅105\max(|x|, |y|, |z|) < |s\_i| \leq 3 \cdot 10^5), consisting of characters a, b, c, d such that s_is\_i has xx, yy, and zz as subsequences.

The sum of ∣s_i∣|s\_i| over all queries does not exceed 3⋅1053 \cdot 10^5.

출력

On the ii-th line, output a single positive integer --- the shortest possible length of a subsequence of s_is\_i that is not a subsequence of any of xx, yy, or zz.

힌트

In the first query, all length 11 subsequences of abcbc are subsequences of one of xx, yy, or zz, but the subsequence cb is not, so the answer is 22.

In the second query, d is a subsequence of dabcabc that is not a subsequence of any of xx, yy, or zz.

In the third query, bbc is a subsequence of abbcc that is not a subsequence of any of xx, yy, or zz, and it is the shortest such subsequence.

예제1

  1. 예제 1

    입력
    abb
    bcc
    abcc
    3
    abcbc
    dabcabc
    abbcc
    
    예상 출력
    2
    1
    3