CF1469A.Regular Bracket Sequence
入门
通过率:0%
时间限制:1.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters + and 1 into this sequence. For example, sequences (())(), () and (()(())) are regular, while )(, (() and (()))( are not. Let's call a regular bracket sequence "RBS".
You are given a sequence s of n characters (, ), 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.
如果可以通过向括号序列中插入字符 + 和 1 来得到一个合法的算术表达式,则称该括号序列为正规括号序列(Regular Bracket Sequence, RBS)。例如,序列 (())()、() 和 (()(())) 是正规括号序列,而 )(、(() 和 (()))( 则不是。
给定一个长度为 n 的字符串 s,其中仅包含字符 (、) 和 ?。该字符串中恰好包含一个 ( 和一个 )。
你需要将每个字符 ? 替换为 ) 或 ((不同的 ? 可以被替换为不同的括号)。你不能对字符进行重排、删除、插入其他字符,且每个 ? 都必须被替换。
请判断:是否存在一种替换方式,使得最终得到的字符串是一个 RBS。
输入格式
The first line contains one integer t (1≤t≤1000) — the number of test cases.
Each test case consists of one line containing s (2≤∣s∣≤100) — a sequence of characters (, ), and/or ?. There is exactly one character ( and exactly one character ) in this sequence.
第一行包含一个整数 t(1≤t≤1000)—— 测试用例的数量。
每个测试用例由一行组成,该行包含字符串 s(2≤∣s∣≤100)—— 一个由字符 (、) 和/或 ? 组成的序列。该序列中恰好包含一个字符 ( 和一个字符 )。
输出格式
For each test case, print YES if it is possible to obtain a regular bracket sequence, or NO otherwise}.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
对于每个测试用例,如果能够得到一个合法的括号序列,则输出 YES,否则输出 NO。
你可以以任意大小写形式输出每个字母(例如,YES、Yes、yes、yEs 均被视为肯定回答)。
输入输出样例
输入#1
5 () (?) (??) ??() )?(?
输出#1
YES NO YES YES NO
说明/提示
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: ()().
在第一个测试用例中,该序列本身已经是一个正则括号序列(RBS)。
在第三个测试用例中,你可以通过如下方式得到一个正则括号序列(RBS):()()或(())`。
在第四个测试用例中,你可以通过如下方式得到一个正则括号序列(RBS):()()`。
输入解题思路,AI测评打分。不知道怎么写?