1. 程式人生 > >【Codeforces Round 336 (Div 2) E】【KMP 構造 腦洞 好題 模擬找規律】Marbles 兩條路徑同時出發前後行走能否恰好同時到終點

【Codeforces Round 336 (Div 2) E】【KMP 構造 腦洞 好題 模擬找規律】Marbles 兩條路徑同時出發前後行走能否恰好同時到終點

In the spirit of the holidays, Saitama has given Genos two grid paths of lengthn(a weird gift even by Saitama's standards). A grid path is an ordered sequence of neighbouring squares in an infinite grid. Two squares are neighbouring if they share a side.

One example of a grid path is(0, 0) → (0, 1) → (0, 2) → (1, 2) → (1, 1) → (0, 1) → ( - 1, 1)

. Note that squares in this sequence might be repeated, i.e. path has self intersections.

Movement within a grid path is restricted to adjacent squares within the sequence. That is, from thei-th square, one canonly moveto the(i - 1)-th or(i + 1)-th squares of this path. Note that there is only a single valid move from the first and last squares of a grid path. Also note, that even if there is somej

-th square of the path that coincides with thei-th square, only moves to(i - 1)-th and(i + 1)-th squares are available. For example, from the second square in the above sequence, one can only move to either the first or third squares.

To ensure that movement is not ambiguous, the two grid paths will not have an alternating sequence of three squares. For example, a contiguous subsequence(0, 0) → (0, 1) → (0, 0)

cannot occurin a valid grid path.

One marble is placed on the first square of each grid path. Genos wants to get both marbles to the last square of each grid path. However, there is a catch. Whenever he moves one marble, the other marble will copy its movement if possible. For instance, if one marble moves east, then the other marble willtryand move east as well. Bytry, we mean if moving east is a valid move, then the marble will move east.

Moving north increases the second coordinate by1, while moving south decreases it by1. Similarly, moving east increases first coordinate by1, while moving west decreases it.

Given these two valid grid paths, Genos wants to know if it is possible to move both marbles to the ends of their respective paths. That is, if it is possible to move the marbles such that both marbles rest on the last square of their respective paths.