C. Bracket Subsequence

本文介绍了一种算法,用于从给定的合法括号序列中构建特定长度的子序列,该子序列同样为合法括号序列。文章通过示例说明了如何选择适当的左括号和右括号来构建所需的子序列。

A bracket sequence is a string containing only characters "(" and ")". 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 (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.

Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

You are given a regular bracket sequence s

and an integer number k. Your task is to find a regular bracket sequence of length exactly k such that it is also a subsequence of s

.

It is guaranteed that such sequence always exists.

Input

The first line contains two integers n

and k (2≤k≤n≤2⋅105, both n and k are even) — the length of s

and the length of the sequence you are asked to find.

The second line is a string s

— regular bracket sequence of length n

.

Output

Print a single string — a regular bracket sequence of length exactly k

such that it is also a subsequence of s

.

It is guaranteed that such sequence always exists.

Examples

Input

Copy

6 4
()(())

Output

Copy

()()

Input

Copy

8 8
(()(()))

Output

Copy

(()(()))


题意:

       给你2个长度,一个字符串,一个是字符串的长度,一个是要你构造的长度,一个字符串合法当且仅当每一位的左括号数大于等于右括号数,且最后一位的左括号数等于右括号数。

思路:

       观察:首先我读懂了题意 提出假设:贪心就行

code:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<stack>
#include<string.h>
# include <cstdio>
# include <iostream>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <ostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n,m,tl,tr,book[200100];
char s[200100];
int main()
{
	memset(book,0,sizeof(book));
	cin>>n>>m;
	scanf("%s",s+1);
	tl=tr=(n-m)/2;
	for(int i=1;i<=n;i++)
	{
		if(tr>0)
		{
			if(s[i]==')')
			{
				book[i]=1;
				tr--;
			}
		}
		else
			break;
	}
	for(int i=1;i<=n;i++)
	{
		if(tl>0)
		{
			if(s[i]=='(')
			{
				book[i]=1;
				tl--;
			}
		}
		else
			break;
	}
	for(int i=1;i<=n;i++)
	{
		if(!book[i])
			cout<<s[i];
	}
	cout<<endl;
	return 0;
} 

 

{"uuid":"6D58260C-3816-42AE-AB44-78687C97C289","mac":"D0-AD-08-60-75-DA"} {"uuid":"6D58260C-3816-42AE-AB44-78687C97C289","mac":"00-FF-43-03-8A-D8"} {"uuid":"6D58260C-3816-42AE-AB44-78687C97C289","mac":"40-A6-B7-C5-5A-3B"} 这个是目前脚本输出的内容,下面是脚本的内容。我希望你对下面的脚本进行优化,每个对象间用逗号分隔 @echo off setlocal enabledelayedexpansion REM Get the directory of this script set "DIR=%~dp0" REM Get the UUID for /f "usebackq tokens=2 delims== " %%A in (`wmic csproduct get uuid /value 2^>nul`) do ( set "UUID=%%A" ) set "UUID=%UUID: =%" if not defined UUID ( echo Failed to retrieve UUID. goto end ) echo UUID: %UUID% REM Clear output file del "%DIR%licence_config.txt" 2>nul echo Collecting MAC addresses... REM Loop through all MAC addresses for /f "skip=1 tokens=1 delims=," %%A in ('getmac /fo csv') do ( set "line=%%A" REM Remove quotes set "line=!line:"=!" REM Extract the first token as MAC address for /f "tokens=1 delims=," %%B in ("!line!") do ( set "mac=%%B" REM Check if MAC matches pattern (already in format XX-XX-XX-XX-XX-XX) echo !mac! | findstr /r "[0-9A-Fa-f][0-9A-Fa-f]-[0-9A-Fa-f][0-9A-Fa-f]-[0-9A-Fa-f][0-9A-Fa-f]-[0-9A-Fa-f][0-9A-Fa-f]-[0-9A-Fa-f][0-9A-Fa-f]-[0-9A-Fa-f][0-9A-Fa-f]" >nul if not errorlevel 1 ( REM Convert to uppercase for %%C in ("!mac!") do set "mac_upper=%%~C" for %%D in ("!mac_upper!") do set "mac_upper=%%~D" REM Print and write the JSON object echo {"uuid":"%UUID%","mac":"!mac_upper!"} echo {"uuid":"%UUID%","mac":"!mac_upper!"} >> "%DIR%licence_config.txt" ) ) ) echo Output complete. Saved to %DIR%licence_config.txt pause :end
最新发布
08-07
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值