Codeforces A. Not a Substring (贪心) (提升编程思维)

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;
}

加油

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值