A15676 | Typical Interview Problem
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The FB-string is formed as follows. Initially, it is empty. We go through all positive integers, starting from $1$ , in ascending order, and do the following for each integer:
- if the current integer is divisible by $3$ , append F to the end of the FB-string;
- if the current integer is divisible by $5$ , append B to the end of the FB-string.
Note that if an integer is divisible by both $3$ and $5$ , we append F, and then B, not in the opposite order.
The first $10$ characters of the FB-string are FBFFBFFBFB: the first F comes from the integer $3$ , the next character (B) comes from $5$ , the next F comes from the integer $6$ , and so on. It's easy to see that this string is infinitely long. Let $f_i$ be the $i$ -th character of FB-string; so, $f_1$ is F, $f_2$ is B, $f_3$ is F, $f_4$ is F, and so on.
You are given a string $s$ , 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 $l$ and $r$ ( $1 \le l \le r$ ) so that the string $f_l f_{l+1} f_{l+2} \dots f_r$ is exactly $s$ .
For example:
- FFB is a substring of the FB-string: if we pick $l = 3$ and $r = 5$ , the string $f_3 f_4 f_5$ is exactly FFB;
- BFFBFFBF is a substring of the FB-string: if we pick $l = 2$ and $r = 9$ , the string $f_2 f_3 f_4 \dots f_9$ is exactly BFFBFFBF;
- BBB is not a substring of the FB-string.
- if the current integer is divisible by $3$ , append F to the end of the FB-string;
- if the current integer is divisible by $5$ , append B to the end of the FB-string.
Note that if an integer is divisible by both $3$ and $5$ , we append F, and then B, not in the opposite order.
The first $10$ characters of the FB-string are FBFFBFFBFB: the first F comes from the integer $3$ , the next character (B) comes from $5$ , the next F comes from the integer $6$ , and so on. It's easy to see that this string is infinitely long. Let $f_i$ be the $i$ -th character of FB-string; so, $f_1$ is F, $f_2$ is B, $f_3$ is F, $f_4$ is F, and so on.
You are given a string $s$ , 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 $l$ and $r$ ( $1 \le l \le r$ ) so that the string $f_l f_{l+1} f_{l+2} \dots f_r$ is exactly $s$ .
For example:
- FFB is a substring of the FB-string: if we pick $l = 3$ and $r = 5$ , the string $f_3 f_4 f_5$ is exactly FFB;
- BFFBFFBF is a substring of the FB-string: if we pick $l = 2$ and $r = 9$ , the string $f_2 f_3 f_4 \dots f_9$ is exactly BFFBFFBF;
- BBB is not a substring of the FB-string.
输入格式
The first line contains one integer $t$ ( $1 \le t \le 2046$ ) — the number of test cases.
Each test case consists of two lines. The first line contains one integer $k$ ( $1 \le k \le 10$ ) — the number of characters in $s$ . The second line contains $s$ , which is a string of exactly $k$ characters. Each character of $s$ is either F or B.
Each test case consists of two lines. The first line contains one integer $k$ ( $1 \le k \le 10$ ) — the number of characters in $s$ . The second line contains $s$ , which is a string of exactly $k$ characters. Each character of $s$ is either F or B.
输出格式
For each test case, print YES if $s$ 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).
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).
输入输出样例
输入 #1
3 3 FFB 8 BFFBFFBF 3 BBB
输出 #1
YES YES NO
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted