CF1796A.Typical Interview Problem

入门

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 (1≤l≤r1 \le l \le r) so that the string flfl+1fl+2…frf_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 f2f3f4…f9f_2 f_3 f_4 \dots f_9 is exactly BFFBFFBF;
  • BBB is not a substring of the FB-string.

FB字符串按如下方式构造:初始时为空。我们从 11 开始,依次遍历所有正整数(升序),对每个整数执行以下操作:

  • 若当前整数能被 33 整除,则在 FB 字符串末尾添加字符 F;
  • 若当前整数能被 55 整除,则在 FB 字符串末尾添加字符 B。

注意:若某个整数同时能被 33 和 55 整除(即能被 1515 整除),则先添加 F,再添加 B,顺序不可颠倒。

FB 字符串的前 1010 个字符为 FBFFBFFBFB:第一个 F 来自整数 33,下一个字符(B)来自 55,再下一个 F 来自整数 66,依此类推。显然,该字符串是无限长的。记 fif_i 为 FB 字符串的第 ii 个字符;因此,f1=Ff_1 = \text{F},f2=Bf_2 = \text{B},f3=Ff_3 = \text{F},f4=Ff_4 = \text{F},等等。

给定一个仅由字符 F 和/或 B 组成的字符串 ss,你需要判断它是否为 FB 字符串的一个子串(即连续子序列)。换言之,需判断是否存在两个整数 ll 和 rr(满足 1≤l≤r1 \le l \le r),使得字符串 flfl+1fl+2…frf_l f_{l+1} f_{l+2} \dots f_r 恰好等于 ss。

例如:

  • FFB 是 FB 字符串的一个子串:取 l=3l = 3、r=5r = 5,则 f3f4f5f_3 f_4 f_5 恰好为 FFB;
  • BFFBFFBF 是 FB 字符串的一个子串:取 l=2l = 2、r=9r = 9,则 f2f3f4…f9f_2 f_3 f_4 \dots f_9 恰好为 BFFBFFBF;
  • BBB 不是 FB 字符串的子串。

输入格式

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

Each test case consists of two lines. The first line contains one integer kk (1≤k≤101 \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.

第一行包含一个整数 tt(1≤t≤20461 \le t \le 2046)—— 测试用例的数量。

每个测试用例由两行组成。第一行包含一个整数 kk(1≤k≤101 \le k \le 10)—— 字符串 ss 的字符个数。第二行包含字符串 ss,其长度恰好为 kk。ss 的每个字符均为 F 或 B。

输出格式

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).

对于每个测试用例,如果 ss 是 FB-字符串的子串,则输出 YES;否则输出 NO。

你可以以任意大小写形式输出每个字母(例如 YES、yes、Yes 均被视为肯定回答,NO、no、nO 均被视为否定回答)。

输入输出样例

  • 输入#1

    3
    3
    FFB
    8
    BFFBFFBF
    3
    BBB

    输出#1

    YES
    YES
    NO

输入解题思路,AI测评打分。不知道怎么写?

首页