QOJ.ac

QOJ

実行時間制限: 2.0 s メモリ制限: 512 MB 満点: 100 ハック可能 ✓

#18137. Palindrome Everywhere

統計

You are given a cycle with $n$ vertices numbered from $0$ to $n - 1$. For each $0 \le i \le n - 1$, there is an undirected edge between vertex $i$ and vertex $((i + 1) \pmod n)$ with the color $c_i$ ($c_i = \text{R}$ or $\text{B}$).

Determine whether the following condition holds for every pair of vertices $(i, j)$ ($0 \le i < j \le n - 1$):

  • There exists a palindrome route between vertex $i$ and vertex $j$. Note that the route may not be simple. Formally, there must exist a sequence $p = [p_0, p_1, p_2, \dots, p_m]$ such that:
    • $p_0 = i$, $p_m = j$;
    • For each $0 \le x \le m - 1$, either $p_{x+1} = (p_x + 1) \pmod n$ or $p_{x+1} = (p_x - 1) \pmod n$;
    • For each $0 \le x \le y \le m - 1$ satisfying $x + y = m - 1$, the edge between $p_x$ and $p_{x+1}$ has the same color as the edge between $p_y$ and $p_{y+1}$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer $n$ ($3 \le n \le 10^6$) — the number of vertices in the cycle.

The second line contains a string $c$ of length $n$ ($c_i = \text{R}$ or $\text{B}$) — the color of each edge.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.

Output

For each test case, print “YES” (without quotes) if there is a palindrome route between any pair of nodes, and “NO” (without quotes) otherwise.

You can output the answer in any case (upper or lower). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will be recognized as positive responses.

Examples

Input 1

7
5
RRRRR
5
RRRRB
5
RBBRB
6
RBRBRB
6
RRBBRB
5
RBRBR
12
RRBRRBRRBRRB

Output 1

YES
YES
YES
NO
NO
YES
NO

Note

In the first test case, it is easy to show that there is a palindrome route between any two vertices.

In the second test case, for any two vertices, there exists a palindrome route with only red edges.

In the third test case, the cycle is as follows: $0 \xrightarrow{\text{R}} 1 \xrightarrow{\text{B}} 2 \xrightarrow{\text{B}} 3 \xrightarrow{\text{R}} 4 \xrightarrow{\text{B}} 0$. Take $(i, j) = (0, 3)$ as an example, then $0 \xrightarrow{\text{R}} 1 \xrightarrow{\text{B}} 2 \xrightarrow{\text{B}} 3 \xrightarrow{\text{R}} 4 \xrightarrow{\text{B}} 0 \xrightarrow{\text{B}} 4 \xrightarrow{\text{R}} 3$ is a palindrome route. Thus, the condition holds for $(i, j) = (0, 3)$.

In the fourth test case, when $(i, j) = (0, 2)$, there does not exist a palindrome route.

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.