大体题意:
给你一个字符串,>字符表示在这个位置你只能向右走,<只能向左走,问你 在1~n 这n 个位置中,有多少位置能掉下去?
思路:
仔细想一想,其实很简单:
如果能从左边掉下去,那必须从某个位置一直向左走,所以 从1号位置开始枚举,枚举到第一个>为止!
如果从右边掉下去,必须从某个位置一直向右走,所以从n 位置一直向左枚举 枚举到第一个 < 为止!
详细见代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 7;
char s[maxn];
int main(){
int n;
scanf("%d%s",&n,s);
int sum = 0;
int p = -1;
while(s[p+1] == '<')++sum,++p;
p = n;
while(s[p-1] == '>')++sum,--p;
printf("%d\n",sum);
return 0;
}
In a new version of the famous Pinball game, one of the most important parts of the game field is a sequence of n bumpers. The bumpers are numbered with integers from 1 to n from left to right. There are two types of bumpers. They are denoted by the characters '<' and '>'. When the ball hits the bumper at position i it goes one position to the right (to the position i + 1) if the type of this bumper is '>', or one position to the left (to i - 1) if the type of the bumper at position i is '<'. If there is no such position, in other words if i - 1 < 1 or i + 1 > n, the ball falls from the game field.
Depending on the ball's starting position, the ball may eventually fall from the game field or it may stay there forever. You are given a string representing the bumpers' types. Calculate the number of positions such that the ball will eventually fall from the game field if it starts at that position.
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the sequence of bumpers. The second line contains the string, which consists of the characters '<' and '>'. The character at the i-th position of this string corresponds to the type of the i-th bumper.
Print one integer — the number of positions in the sequence such that the ball will eventually fall from the game field if it starts at that position.
4 <<><
2
5 >>>>>
5
4 >><<
0
In the first sample, the ball will fall from the field if starts at position 1 or position 2.
In the second sample, any starting position will result in the ball falling from the field.