#P10653. Segment Transformation
Segment Transformation
Segment Transformation
Given a string $S$ of length $n$ consisting only of the characters '+' and '-', we define a segment as a substring $S[l,r]$ (with $1\le l\le r\le n$) satisfying:
- $l=1$ or $S_{l-1}\neq S_l$,
- $r=n$ or $S_{r+1}\neq S_r$, and
- $S_l=S_{l+1}=\cdots=S_r$.
A transformation is defined as follows:
- Select two adjacent segments in $S$ whose lengths are different. Let their lengths be $L_1$ and $L_2$; then change every character of the segment with the smaller length to its opposite (i.e. change '+' to '-' and '-' to '+').
You are given $q$ queries. In each query, you are given two strings $S_i$ and $T_i$ (both of the same length and each consisting solely of '+' and '-'). Determine whether $S_i$ can be transformed into $T_i$ by applying a sequence of the described transformations.
Note: A string is frozen if all its segments have the same length, in which case no transformation is allowed (and $S$ can only be transformed to itself). Otherwise, repeated transformations will eventually merge all segments into a uniform string (all characters identical). In particular, if $S$ is not frozen, then the only possible transformation result is a string where every character is equal to the character of the longer segment in the first encountered eligible adjacent pair.
Formally, define the segments of $S$, and let the first adjacent pair of segments with different lengths be $(c_1, L_1)$ and $(c_2, L_2)$. The final uniform state obtainable from $S$ will be:
[ U = \begin{cases} \underbrace{c_2 c_2 \cdots c_2}{n}, & \text{if } L_1 < L_2,\[8pt] \underbrace{c_1 c_1 \cdots c_1}{n}, & \text{if } L_1 > L_2. \end{cases} ]
You should output YES
if $T_i$ is reachable from $S_i$, or NO
otherwise.
inputFormat
The first line of input contains an integer $q$ ($1\le q\le 10^5$), the number of queries. The following lines describe the queries. For each query, there are two lines:
- The first line contains the string $S$, consisting solely of '+' and '-'.
- The second line contains the string $T$, also consisting solely of '+' and '-' and having the same length as $S$.
Note that the total length of all strings does not exceed $10^5$.
outputFormat
For each query, output a single line containing YES
if $T$ is reachable from $S$, or NO
otherwise.
sample
3
++-
+++
++--
++--
+--+
----
YES
YES
YES
</p>