1428B.md (2376B)
1 Source: https://codeforces.com/problemset/problem/1428/B 2 3 B. Belted Rooms 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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. 22 23 The $i$-th conveyor belt is in one of three states: 24 25 * If it is clockwise, snakes can only go from room $i$ to $(i+1) \bmod n$. 26 * If it is anticlockwise, snakes can only go from room $(i+1) \bmod n$ to $i$. 27 * If it is off, snakes can travel in either direction. 28 29  30 31 Above is an example with $4$ rooms, where belts $0$ and $3$ are off, $1$ is clockwise, and $2$ is anticlockwise. 32 33 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? 34 35 Input 36 37 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. 38 39 The first line of each test case description contains a single integer $n$ ($2 \le n \le 300\,000$): the number of rooms. 40 41 The next line of each test case description contains a string $s$ of length $n$, consisting of only '<', '>' and '-'. 42 43 * If $s_{i} = $ '>', the $i$-th conveyor belt goes clockwise. 44 * If $s_{i} = $ '<', the $i$-th conveyor belt goes anticlockwise. 45 * If $s_{i} = $ '-', the $i$-th conveyor belt is off. 46 47 It is guaranteed that the sum of $n$ among all test cases does not exceed $300\,000$. 48 49 Output 50 51 For each test case, output the number of returnable rooms. 52 53 Example 54 55 Input 56 57 ``` 58 4 59 4 60 -><- 61 5 62 >>>>> 63 3 64 <-- 65 2 66 <> 67 ``` 68 69 Output 70 71 ``` 72 3 73 5 74 3 75 0 76 ``` 77 78 Note 79 80 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. 81 82 In the second test case, all rooms are returnable by traveling on the series of clockwise belts.