洛谷|CF1469A Regular Bracket Sequence

题目描述

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 ss of nn 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.

输入格式

The first line contains one integer tt ( 1 \le t \le 10001≤t≤1000 ) — the number of test cases.

Each test case consists of one line containing ss ( 2 \le |s| \le 1002≤∣s∣≤100 ) — a sequence of characters (, ), and/or ?. There is exactly one character ( and exactly one character ) in this sequence.

输出格式

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).

题意翻译

多测。

给定一个包含 (,),? 三种字符的字符串,并且保证只有一个 ( 和一个 ) 。

其中你可以将 ? 替换成 ( or )

问是否存在一种替换方案,使得替换后的串左右括号匹配。

注意 ? 必须得替换掉,最后留下的必须是个括号串。

存在输出一行 YES,否则输出一行 NO

输入输出样例

输入 #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: ()().

 

#include<stdio.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		string s;
		cin>>s;
		int len=s.length();
		if(len%2!=0||s[0]==')'||s[len-1]=='(')
		{
			cout<<"NO"<<endl;
		}
		else
		{
			cout<<"YES"<<endl;
		}
	}
	
	
	
	
	
	
	
	
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值