QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 2048 MB Total points: 100 Hackable ✓

#21437. 兩步之遙

Statistics

有一台專門處理 $\texttt{01}$ 串的字串變換機,其內部預設了一個包含若干條規則的有限集合 $\mathcal{R}$。每條規則形如 $(u, v)$,其中 $u, v$ 是非空等長的 $\texttt{01}$ 串,表示在一步操作中,可以選擇當前字串的一個子字串 $u$,並將其替換為 $v$。每次向機器輸入一對非空等長 $\texttt{01}$ 串 $(s, t)$,機器都會輸出將 $s$ 變換為 $t$ 所需的最少操作步數,或報告無解。

你回憶起自己曾向這台機器輸入過 $n$ 對非空等長 $\texttt{01}$ 串 $(s_1, t_1), (s_2, t_2), \dots, (s_n, t_n)$。巧合的是,在你的記憶中,每次詢問不僅都有解,而且滿足最少操作步數 $d_i \leqslant 2$。

為了驗證你的記憶是否出現偏差,請你判斷:是否存在一個規則集 $\mathcal{R}$,使得機器對這 $n$ 組輸入的實際輸出與你的記憶完全一致?

輸入格式

第一行包含一個正整數 $T$,表示測試資料組數。

對於每組測試資料,第一行包含一個正整數 $n$,表示詢問記錄的數量。接下來 $n$ 行,第 $i$ 行包含兩個非空等長 $\texttt{01}$ 串 $s_i, t_i$ 和一個整數 $d_i$($0 \leqslant d_i \leqslant 2$),分別表示第 $i$ 次輸入的初始串、目標串以及機器給出的最少操作步數。

輸出格式

對於每組測試資料,輸出一行一個字串。若存在滿足條件的規則集 $\mathcal{R}$,則輸出 YES;否則輸出 NO。

你可以輸出 YES 和 NO 的任意大小寫形式,例如 yes、Yes 或 YES 均會被視為相同答案。

範例

輸入格式 1

3
3
010 010 0
001 010 1
001 100 2
2
10 01 1
100 010 2
1
0 1 2

輸出格式 1

YES
NO
NO

說明

  • 第 $1$ 組測試資料:可以構造只包含一條規則的集合 $\mathcal{R} = \{(\texttt{01}, \texttt{10})\}$:
    • 對於第 $1$ 次詢問,$s_1 = t_1 = \texttt{010}$,無需任何操作即可完成變換,最少操作步數為 $0$。
    • 對於第 $2$ 次詢問,將 $\texttt{001}$ 的後綴 $\texttt{01}$ 替換為 $\texttt{10}$,即可在 $1$ 步內得到 $\texttt{010}$。
    • 對於第 $3$ 次詢問,由於 $\texttt{001}$ 與 $\texttt{100}$ 在第 $1$ 位和第 $3$ 位均不同,僅靠長度為 $2$ 的規則無法在 $1$ 步內完成變換;但可以先將初始串 $\texttt{001}$ 的後綴 $\texttt{01}$ 替換為 $\texttt{10}$ 得到中間串 $\texttt{010}$,再將中間串 $\texttt{010}$ 的前綴 $\texttt{01}$ 替換為 $\texttt{10}$ 得到目標串 $\texttt{100}$,因此最少操作步數為 $2$。
  • 第 $2$ 組測試資料:為了在 $1$ 步內將 $\texttt{10}$ 變換為 $\texttt{01}$,規則集 $\mathcal{R}$ 中必須包含規則 $(\texttt{10}, \texttt{01})$。然而一旦包含該規則,第 $2$ 次詢問只需將 $\texttt{100}$ 的前綴 $\texttt{10}$ 替換為 $\texttt{01}$,即可在 $1$ 步內得到 $\texttt{010}$,與記憶中的 $d_2 = 2$ 矛盾。
  • 第 $3$ 組測試資料:變換過程中的所有字串均須為等長的 $\texttt{01}$ 串。長度為 $1$ 的 $\texttt{01}$ 串僅有 $\texttt{0}$ 和 $\texttt{1}$ 兩種,若能從 $\texttt{0}$ 變換為 $\texttt{1}$,則必然存在規則 $(\texttt{0}, \texttt{1})$ 並且只需 $1$ 步即可完成,不可能最少需要 $2$ 步。

資料範圍

對於所有測試資料,保證 $T \geqslant 1$,$n \geqslant 1$,$0 \leqslant d_i \leqslant 2$,$1 \leqslant |s_i| = |t_i|$,且 $s_i, t_i$ 均為僅包含字元 $\texttt{0}$ 和 $\texttt{1}$ 的字串。

記 $L = \sum_{i=1}^{n} |s_i|$ 為單組測試資料中所有初始串的長度之和,$\sum L$ 為單個測試點內所有測試資料的 $L$ 之和,則保證 $\sum L \leqslant 3\times 10^6$。

本題開啟合理的子任務依賴。

子任務編號 分值 $\sum L \leqslant$ 特殊性質
$1$ $16$ $4 \times 10^3$ A
$2$ $20$ $3\times 10^6$ A
$3$ $20$ $2\times 10^4$ 無
$4$ $28$ $10^5$ 無
$5$ $16$ $3 \times 10^6$ 無
  • 特殊性質 A:保證對於所有 $1 \leqslant i \leqslant n$,均有 $|s_i| \geqslant 50$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.