当前位置:网站首页>C. Recover an RBS
C. Recover an RBS
2022-07-24 15:15:00 【beyond+myself】
Topic link
The question : Is to give a sequence of parentheses , It only contains ’(‘,’)‘,’?‘, here ’?‘ It can be for ‘(’ or ‘)’ , Ask whether the sequence is only one correct parenthesis sequence ( In the question, ensure that each string has at least one correct bracket sequence ).
Answer key : We can analyze several properties of this problem :
1: Ensure that there is at least one correct bracket sequence in the question , So don't consider the situation that doesn't exist .
2: There is no requirement to find all possible parenthesis sequences , Just find out if there is only one correct bracket sequence , So we can find all possible sequences of parentheses to see if they are 1; You can also find two possible sequences and output "NO", You can also find out if you can't find another result on the basis of the original strategy , It outputs "YES"
2: There is only one kind of bracket in the question , So don't consider something like ( [ ) ] This bracket situation , So the preceding ’(‘ It must be able to eliminate the latter one ’)‘, That is, when the number of whole brackets is the same , In a section , Ahead ‘(’ As long as it's better than the back ‘)’ Just more , Because every paragraph ‘(’ Can destroy the next section ‘)’ , Then continue to destroy the following ‘)’ .
After analyzing the above properties , We can determine a strategy that can be sure to succeed , That is to put all ‘(’ They are all placed in front in turn ’?‘ in , So under the condition of this question , There must be a strategy that can match the success .
Let's analyze how to find another answer that may match successfully , Is to let in front of and behind ‘(’ and ‘)’ If the quantitative impact is as small as possible, it is to see whether there is still a strategy that may match successfully .
How to minimize the quantitative impact of both ?
Is to exchange two different parentheses closest to each other, which is the original ’?‘, This will ensure that the impact is minimal , Because each exchange will affect the relative number of left and right parentheses in that paragraph , Suppose a far away ’)’ With the current ‘(’ swapping , This will affect a wide range , Probably the one far away ’)' It will be the last bracket . If we can't understand it like this, we can understand it like this , Our best strategy every time is to put the left bracket to the left as far as possible , The right bracket should be placed to the right as far as possible . And our current step must be to replace a left bracket with a right bracket , under these circumstances , Our best case is to take two recently different brackets , This ensures that the last left parenthesis on the left is placed in the first right parenthesis ; The first right parenthesis is placed at the position of the last left parenthesis on the left . This situation must be the best strategy in the current situation .
Here is AC Code :
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
const int N=2e5+10;
char s[N];
vector<int> l;
vector<int> r;
int main()
{
int t;
cin>>t;
while(t--)
{
l.clear();
r.clear();
cin>>s+1;
int len=strlen(s+1);
int cntl=0,cntr=0;
for(int i=1;i<=len;i++)
{
if(s[i]=='(') cntl++;
if(s[i]==')') cntr++;
}
cntl=len/2-cntl;
cntr=len/2-cntr;
if(cntl==0||cntr==0)
{
printf("YES\n");
continue;
}
for(int i=1;i<=len;i++)
{
if(cntl==0) break;
if(s[i]=='?')
{
cntl--;
l.push_back(i);
}
}
for(int i=0;i<l.size();i++)
{
int x=l[i];
s[x]='(';
}
for(int i=l.back()+1;i<=len;i++)
{
if(cntr==0) break;
if(s[i]=='?')
{
cntr--;
r.push_back(i);
}
}
for(int i=0;i<r.size();i++)
{
int x=r[i];
s[x]=')';
}
swap(s[l.back()],s[r.front()]);
// Judge whether it is possible
int cnt=0;
bool flag=false;
for(int i=1;i<=len;i++)
{
if(s[i]=='(') cnt++;
if(s[i]==')') cnt--;
if(cnt<0)
{
flag=true;
break;
}
}
if(flag==true) printf("YES\n");
else printf("NO\n");
}
return 0;
}
summary : The key to solving this problem is that any bracket in front of it can eliminate the bracket behind it , It is different from the bracket matching encountered before , therefore , Such questions must be specific , make a concrete analysis . Find out the unique properties of this problem , Never set your mind .
边栏推荐
- Kotlin class and inheritance
- JS data transformation -- Transformation of tree structure and tile structure
- How to set packet capturing mobile terminal
- DS inner row heap sort
- Conflict resolution of onblur and onchange
- PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
- VAE(变分自编码器)的一些难点分析
- Leetcode-09 (next rank + happy number + full rank)
- Overall testing framework for performance testing
- Comparison of traversal speed between map and list
猜你喜欢

ZABBIX administrator forgot login password

Jmmert aggregation test report
![[bug solution] error in installing pycocotools in win10](/img/91/4d0ed64738656a6f406f760d6bece3.png)
[bug solution] error in installing pycocotools in win10

(09) flask is OK if it has hands - cookies and sessions

Existence form and legitimacy of real data in C language (floating point number)

文件操作详解

Route planning method for UAV in unknown environment based on improved SAS algorithm

VSCode如何调试Nodejs

Spark Learning Notes (III) -- basic knowledge of spark core

Operation of MySQL Library
随机推荐
Preparation of mobile end test cases
【USENIX ATC'22】支持异构GPU集群的超大规模模型的高效的分布式训练框架Whale
Huawei wireless device configuration wpa2-802.1x-aes security policy
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第三题 跑团机器人 (已完结)
Summary of feature selection: filtered, wrapped, embedded
A common Dao class and util
【Bug解决】Win10安装pycocotools报错
“00后”来了!数睿数据迎来新生代「无代码」生力军
Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
Fraud detection cases and Titanic rescued cases
Explain the edge cloud in simple terms | 2. architecture
Which brokerage has the lowest commission? I want to open an account. Is it safe to open an account on my mobile phone
pytorch with torch.no_ grad
[tkinter美化] 脱离系统样式的窗口(三系统通用)
pytorch with torch.no_grad
DS graph - minimum spanning tree
Data analysis and mining 1
C# SQLite Database Locked exception
25.从生磁盘到文件
DDD based on ABP -- Entity creation and update