#P6652. 「SWTR-5」String

    ID: 7022 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DPO2优化广度优先搜索 BFS哈希 hashing

「SWTR-5」String

Problem Description

Little A has a string tt. He can do the following operation: cut off a prefix or suffix of tt, with the condition that the removed prefix/suffix is a substring of tt after the cut. Little A wants to obtain the string ss. What is the minimum number of operations needed? If it is impossible, output 1-1.

Input Format

Two lines, each containing a string, representing tt and ss.

Output Format

Output one integer in one line, representing the answer.

abbabb
ba
3
fxofoxxooffoxooo
fox
8
abcdefghijklmnopq
rstuvwxyzz
-1
ycxcy
cxy
-1

Hint

"Sample Explanation"

Sample 11: $\texttt{abbabb}\to \texttt{abba}\to \texttt{bba}\to \texttt{ba}$. The solution is not unique.

Sample 22: $\texttt{fxofoxxooffoxooo}\to\texttt{xofoxxooffoxooo}\to\texttt{foxxooffoxooo}\to\texttt{xooffoxooo}\to\texttt{ffoxooo}\to\texttt{ffoxoo}\to\texttt{ffoxo}\to\texttt{ffox}\to\texttt{fox}$. The solution is not unique.

"Constraints and Notes"

This problem uses bundled tests.

  • Subtask 1 (1 points): s=ts=t.
  • Subtask 2 (9 points): ss contains only the letter a\texttt{a}.
  • Subtask 3 (15 points): t100|t|\leq 100.
  • Subtask 4 (17 points): t500|t|\leq 500.
  • Subtask 5 (18 points): t1.5×103|t|\leq 1.5\times 10^3.
  • Subtask 6 (15 points): s=4|s|=4, *testdata is random.
  • Subtask 7 (25 points): No special restrictions.

For 100%100\% of the testdata: 1st5×1031 \leq |s| \leq |t| \leq 5\times 10^3, character set [a,z]\in[\texttt{a,z}].

*Random testdata: the characters of s,ts,t are random, character set [a,c]\in[\texttt{a,c}].

Please pay attention to constant-factor optimization.


"Source"

Sweet Round 05 E。
idea & solution: Isaunoya & Alex_Wei

Translated by ChatGPT 5