#P15854. [蓝桥杯第二届国际赛] 基因子序列

[蓝桥杯第二届国际赛] 基因子序列

Problem Description

A biological gene is made up of 44 different bases, usually denoted by A, T, G, and C. A gene can be represented as a sequence of bases in order, for example, ATCACAGGT.

Xiaoming has recently been paying attention to a special base sequence SS (and SS is also composed of A, T, G, and C). He found that if, in a gene, we can extract some bases in their order of appearance and make them exactly equal to SS, then the gene may have some property. For example, when S=TCGS=\text{TCG}, we can extract the 22nd, 33rd, and 77th bases from the gene ATCACAGGT to match SS, but we cannot extract a part of the gene CGT to match SS.

Of course, the extraction positions can be varied. For instance, we can extract the 22nd, 55th, and 88th bases from the gene ATCACAGGT to match SS. Xiaoming wants to know: among all ways that can make them equal, what is the minimum possible index of the last extracted base in the gene.

Input Format

The first line contains a string SS, representing the given base sequence.

The second line contains a string DD, representing the given gene.

Output Format

If no valid extraction method can be found, output −1-1. Otherwise, output the index of the last extracted base in the gene when they are equal.

TCG
ATCACAGGT
7

Hint

Sample Explanation

This problem asks for the minimum answer. If you output 88, it is incorrect.

Constraints

For 40%40\% of the test cases, the lengths of both strings do not exceed 10001000.

For all test cases, the lengths of both strings do not exceed 100000100000. The characters that appear are only A, T, C, and G.

Translated by ChatGPT 5