CF1796A.Typical Interview Problem

传统题 时间 2000 ms 内存 256 MiB 3 尝试 2 已通过 1 标签

Typical Interview Problem

The FB-string is formed as follows. Initially, it is empty. We go through all positive integers, starting from 11, in ascending order, and do the following for each integer:

  • if the current integer is divisible by 33, append F to the end of the FB-string;
  • if the current integer is divisible by 55, append B to the end of the FB-string.

Note that if an integer is divisible by both 33 and 55, we append F, and then B, not in the opposite order.

The first 1010 characters of the FB-string are FBFFBFFBFB: the first F comes from the integer 33, the next character (B) comes from 55, the next F comes from the integer 66, and so on. It's easy to see that this string is infinitely long. Let fif_i be the ii-th character of FB-string; so, f1f_1 is F, f2f_2 is B, f3f_3 is F, f4f_4 is F, and so on.

You are given a string ss, consisting of characters F and/or B. You have to determine whether it is a substring (contiguous subsequence) of the FB-string. In other words, determine if it is possible to choose two integers ll and rr (1lr1 \le l \le r) so that the string flfl+1fl+2frf_l f_{l+1} f_{l+2} \dots f_r is exactly ss.

For example:

  • FFB is a substring of the FB-string: if we pick l=3l = 3 and r=5r = 5, the string f3f4f5f_3 f_4 f_5 is exactly FFB;
  • BFFBFFBF is a substring of the FB-string: if we pick l=2l = 2 and r=9r = 9, the string f2f3f4f9f_2 f_3 f_4 \dots f_9 is exactly BFFBFFBF;
  • BBB is not a substring of the FB-string.

Input

The first line contains one integer tt (1t20461 \le t \le 2046) — the number of test cases.

Each test case consists of two lines. The first line contains one integer kk (1k101 \le k \le 10) — the number of characters in ss. The second line contains ss, which is a string of exactly kk characters. Each character of ss is either F or B.

Output

For each test case, print YES if ss is a substring of the FB-string, or NO otherwise.

You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).

Samples

3
3
FFB
8
BFFBFFBF
3
BBB
YES
YES
NO

在线编程 IDE

建议全屏模式获得最佳体验