codeforces-1023 C Bracket Subsequence

本文介绍了一种生成平衡括号字符串的算法实现。通过控制左括号与右括号的数量,确保最终输出的字符串中左右括号数量相等且配对正确。该程序使用C++编写,并展示了如何读取输入的长度和最大不平衡度,然后输出符合要求的最短平衡括号字符串。
 1 #include <iostream>
 2 #include <unordered_map>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <queue>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     int n,k;
12     while(cin >> n >> k)
13     {
14         string input;
15         cin >> input;
16         string result;
17         int right_existed = 0;
18         int left = 0;
19         int right = 0;
20         for(int i = 0; i < n; i ++)
21         {
22             if(right_existed!=k/2)
23             {
24                 result += input[i];
25                 if(input[i]=='(')
26                 {
27                     right_existed ++;
28                     left ++;
29                 }
30                 else
31                     right ++;
32             }
33         }
34         int tmp = left - right;
35         while(tmp--)
36             result += ')';
37         cout << result << endl;
38     }
39     return 0;
40 }

 

转载于:https://www.cnblogs.com/Asurudo/p/9496348.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值