#P1032. [NOIP 2002 提高组] 字串变换(疑似错题)

    ID: 32 远端评测题 1000ms 125MiB 尝试: 6 已通过: 3 难度: 6 上传者: 标签>字符串搜索2002NOIp 提高组广度优先搜索,BFS剪枝

[NOIP 2002 提高组] 字串变换(疑似错题)

Description

You are given two strings A,BA, B and a set of string transformation rules (at most 66 rules), of the form:

  • A1B1A_1 \to B_1.
  • A2B2A_2 \to B_2.

The meaning of the rules is: in AA, a substring A1A_1 can be transformed into B1B_1, A2A_2 can be transformed into B2B_2 \cdots.

For example: A=abcdA=\texttt{abcd}, B=xyzB=\texttt{xyz},

with the transformation rules:

  • abcxu\texttt{abc}\rightarrow\texttt{xu}, udy\texttt{ud}\rightarrow\texttt{y}, yyz\texttt{y}\rightarrow\texttt{yz}.

Then AA can be transformed into BB through a sequence of transformations:

  • $\texttt{abcd}\rightarrow\texttt{xud}\rightarrow\texttt{xy}\rightarrow\texttt{xyz}$.

A total of 33 transformations are performed to transform AA into BB.

Input Format

The first line contains two strings A,BA, B.

Each of the following lines contains two strings Ai,BiA_i, B_i, representing one transformation rule.

Output Format

If AA can be transformed into BB within 1010 steps (inclusive), output the minimum number of steps; otherwise, output NO ANSWER!.

abcd xyz
abc xu
ud y
y yz

3

Hint

For 100%100\% of the testdata, the maximum length of all strings is 2020.

Source: NOIP 2002 Senior, Problem 2.

Translated by ChatGPT 5