A. Regular Bracket Sequence
技術標籤:AC路漫漫
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters+and1into this sequence. For example, sequences(())(),()and(()(()))are regular, while)(,(()and(()))(are not. Let's call a regular bracket sequence "RBS".
You are given a sequencesofncharacters(,), and/or?.There is exactly one character(and exactly one character)in this sequence.
You have to replace every character?with either)or((different characters?can be replaced with different brackets).You cannot reorder the characters, remove them, insert other characters, and each?must be replaced.
Determine if it is possible to obtain an RBS after these replacements.
Input
The first line contains one integert(1≤t≤1000) — the number of test cases.
Each test case consists of one line containings(2≤|s|≤100) — a sequence of characters(,), and/or?.There is exactly one character(and exactly one character)in this sequence.
Output
For each test case, printYESif it is possible to obtain a regular bracket sequence, orNOotherwise}.
You may print each letter in any case (for example,YES,Yes,yes,yEswill all be recognized as positive answer).
Example
input
Copy
5 () (?) (??) ??() )?(?
output
Copy
YES NO YES YES NO
Note
In the first test case, the sequence is already an RBS.
In the third test case, you can obtain an RBS as follows:()()or(()).
In the fourth test case, you can obtain an RBS as follows:()().
解題說明:水題,先統計字元總數,當總數為奇數並且頭尾兩個括號位置和正常相反就不符合題意。
#include<stdio.h>
int main()
{
int T, i, j;
char s[101];
scanf("%d", &T);
for (i = 0; i < T; i++)
{
scanf("%s", s);
j = 0;
while (s[j] != '\0')
{
j++;
}
if (j % 2 == 1 || s[0] == ')' || s[j - 1] == '(')
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
return 0;
}