A13794 | Belted Rooms
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
In the snake exhibition, there are $n$ rooms (numbered $0$ to $n - 1$ ) arranged in a circle, with a snake in each room. The rooms are connected by $n$ conveyor belts, and the $i$ -th conveyor belt connects the rooms $i$ and $(i+1) \bmod n$ . In the other words, rooms $0$ and $1$ , $1$ and $2$ , $\ldots$ , $n-2$ and $n-1$ , $n-1$ and $0$ are connected with conveyor belts.
The $i$ -th conveyor belt is in one of three states:
- If it is clockwise, snakes can only go from room $i$ to $(i+1) \bmod n$ .
- If it is anticlockwise, snakes can only go from room $(i+1) \bmod n$ to $i$ .
- If it is off, snakes can travel in either direction.
Above is an example with $4$ rooms, where belts $0$ and $3$ are off, $1$ is clockwise, and $2$ is anticlockwise.
Each snake wants to leave its room and come back to it later. A room is returnable if the snake there can leave the room, and later come back to it using the conveyor belts. How many such returnable rooms are there?
The $i$ -th conveyor belt is in one of three states:
- If it is clockwise, snakes can only go from room $i$ to $(i+1) \bmod n$ .
- If it is anticlockwise, snakes can only go from room $(i+1) \bmod n$ to $i$ .
- If it is off, snakes can travel in either direction.
Above is an example with $4$ rooms, where belts $0$ and $3$ are off, $1$ is clockwise, and $2$ is anticlockwise.
Each snake wants to leave its room and come back to it later. A room is returnable if the snake there can leave the room, and later come back to it using the conveyor belts. How many such returnable rooms are there?
输入格式
Each test contains multiple test cases. The first line contains a single integer $t$ ( $1 \le t \le 1000$ ): the number of test cases. The description of the test cases follows.
The first line of each test case description contains a single integer $n$ ( $2 \le n \le 300\,000$ ): the number of rooms.
The next line of each test case description contains a string $s$ of length $n$ , consisting of only '<', '>' and '-'.
- If $s_{i} = $ '>', the $i$ -th conveyor belt goes clockwise.
- If $s_{i} = $ '<', the $i$ -th conveyor belt goes anticlockwise.
- If $s_{i} = $ '-', the $i$ -th conveyor belt is off.
It is guaranteed that the sum of $n$ among all test cases does not exceed $300\,000$ .
The first line of each test case description contains a single integer $n$ ( $2 \le n \le 300\,000$ ): the number of rooms.
The next line of each test case description contains a string $s$ of length $n$ , consisting of only '<', '>' and '-'.
- If $s_{i} = $ '>', the $i$ -th conveyor belt goes clockwise.
- If $s_{i} = $ '<', the $i$ -th conveyor belt goes anticlockwise.
- If $s_{i} = $ '-', the $i$ -th conveyor belt is off.
It is guaranteed that the sum of $n$ among all test cases does not exceed $300\,000$ .
输出格式
For each test case, output the number of returnable rooms.
输入输出样例
输入 #1
4 4 -><- 5 >>>>> 3 <-- 2 <>
输出 #1
3 5 3 0
In the first test case, all rooms are returnable except room $2$ . The snake in the room $2$ is trapped and cannot exit. This test case corresponds to the picture from the problem statement.
In the second test case, all rooms are returnable by traveling on the series of clockwise belts.
In the second test case, all rooms are returnable by traveling on the series of clockwise belts.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted