1140B B. Good String

You have a string s of length n consisting of only characters > and <. You may do some operations with this string, for each operation you have to choose some character that still remains in the string. If you choose a character >, the character that comes right after it is deleted (if the character you chose was the last one, nothing happens). If you choose a character <, the character that comes right before it is deleted (if the character you chose was the first one, nothing happens).

For example, if we choose character > in string > > < >, the string will become to > > >. And if we choose character < in string > <, the string will become to <.

The string is good if there is a sequence of operations such that after performing it only one character will remain in the string. For example, the strings >, > > are good.

Before applying the operations, you may remove any number of characters from the given string (possibly none, possibly up to n−1, but not the whole string). You need to calculate the minimum number of characters to be deleted from string s so that it becomes good.

Input
The first line contains one integer t (1≤t≤100) – the number of test cases. Each test case is represented by two lines.

The first line of i-th test case contains one integer n (1≤n≤100) – the length of string s.

The second line of i-th test case contains string s, consisting of only characters > and <.

Output
For each test case print one line.

For i-th test case print the minimum number of characters to be deleted from string s so that it becomes good.

Example
input

3
2
<>
3

<<
1

output
1
0
0
Note
In the first test case we can delete any character in string <>.

In the second test case we don’t need to delete any characters. The string > < < is good, because we can perform the following sequence of operations: > < < → < < → <.
题目大意: t组测试用例中,每组字符的长度为n,下一行为一个只包含’<‘和’>‘字符的字符串,如果选择’<‘字符,则可以删除前一个字符(最开头的无效),如果选择’>‘字符,则可以删除后一个字符(结尾的无效),最终要使字符串变为只含’<‘或’>‘的字符,问至少要删除几个字符。注意:对于一个字符串中内部的操作只能选择这两个中的一个进行删除,一开始以为两个都能选,结果WA~
思路: 由题意可知,如果开头是’>’,后面的就可以一直内部操作删除,如果是’<'结尾,前面的可以一直内部操作删除,该题着重考虑的情况是<<<<>>>这种类型的,由于只能选择其中一个进行内部操作,于是只要找出其两个子区间的长度,输出最小的那一个就行了,具体的看代码。

#include 
#include 
using namespace std;
int main() {
	int t, n, a, b, ans;
	string s;
	scanf("%d", &t);
	while(t--) {
		scanf("%d", &n);
		getchar(); // 获取换行符 
		cin >> s;
		if(n == 1) { // 字符串长度为1,直接输出0 
			printf("0\n");
		} else if(n == 2) { // 长度为2是,只有<>要删除一个,其他的都不用删除 
			if(s[0] == '>' || s[1] == '<') printf("0\n");
			else printf("1\n");
		} else {
			if(s[0] == '>' || s[n-1] == '<') printf("0\n"); // 如果开头是'>',后面的可以一直删除,如果是'<'结尾的,前面的可以一直删除 
			else {
				for(int i = 1; i < n; i++) { // 遍历找出<序列的长度 
					if(s[i] == '>') {
						a = i;
						break;
					}
				}
				for(int i = n - 1; i > 0; i--) { // 遍历找出>的开始下标 
					if(s[i] == '<') {
						b = i;
						break;
					}
				}
				b = n - 1 - b; // 算出>序列的长度 
				ans = min(a, b); // 取最小值 
				printf("%d\n", ans);	
			}
		}
	}
	return 0;
}

你可能感兴趣的:(codeforces)