A bracket sequence is a string consisting of characters '(' and/or ')'. A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example:
- bracket sequences "()()" and "(())" are regular (they can be transformed into "(1)+(1)" and "((1+1)+1)", respectively);
- bracket sequences ")(", "(" and ")" are not regular.
You are given a bracket sequence ss; let's define its length as n. Your task is to find a regular bracket sequence tt of length 2n such that ss does not occur in tt as a contiguous substring, or report that there is no such sequence.
Input
The first line contains a single integer tt (1≤t≤1000) — the number of test cases.
The only line of each test case contains a string ss (2≤|s|≤50), consisting of characters "(" and/or ")".
Output
For each test case, print the answer to it. If there is no required regular bracket sequence, print NO in a separate line. Otherwise, print YES in the first line, and the required regular bracket sequence tt itself in the second line. If there are multiple answers — you may print any of them.
题意
输入一个整数t,代表要进行4次,每次输入一个只包含'('、')'的字符串s,如果说正则匹配了(例如"()()"、"((()))"),那么直接输出“NO”,如果没有匹配,我们需要让他变成正则匹配,长度要变成2 * s.size(),并且字串不包含s
思路:
这个问题需要好好思考,正则匹配无非就是 a:"()()()()()"、b:"(((())))" 这两种形式的组合,那我们首先把给定的s的长度求出来,然后得出对应的a和b,到a和b里面去找一下有没有s这个字串,如果有,说明一定正则匹配了,没有,就输出对应的a || b就行了
C++代码:
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int t;
int main()
{
cin >> t;
while(t --){
string s,a = "",b = "";
cin >> s;
for(int i = 0;i < s.size();i ++) a += "()",b += '(';
for(int i = s.size();i < s.size() * 2;i ++) b += ')';
if(a.find(s) == string::npos) cout << "YES" << endl << a << endl;
else if(b.find(s) == string::npos) cout << "YES" << endl << b << endl;
else cout << "NO" << endl;
}
return 0;
}
加油