当前位置:网站首页>C. Recover an RBS
C. Recover an RBS
2022-07-24 15:13:00 【beyond+myself】
题目链接
题意:就是给出一个括号序列,其中只包含’(‘,’)‘,’?‘,这里’?‘可以为 ‘(’ 或 ‘)’ ,问是否该序列是只有一个正确的括号序列(题中保证每个字符串至少有一种正确的括号序列)。
题解:我们可以分析此题的几个性质:
1:题中保证至少有一种正确的括号序列,所以不用去考虑不存在的情况。
2:题中没有要求求出所有的可能的括号序列,只要求求出是不是只有一种正确的括号序列,所以我们可以求出所有的可能的括号序列看是否为1;也可以找出两个可能的序列然后输出"NO",也可以找出原策略的基础上影响最小的情况下如果还找不到另一种结果,就输出"YES"
2:题中只有一种括号,所以不用考虑类似于( [ ) ] 这种括号的情况,所以前面的’(‘一定就是可以消除后面的一个’)‘,也就是在整体括号的数量相同的情况下,在一段区间内,前面的 ‘(’ 只要比后面的 ‘)’ 多即可,因为每一段的 ‘(’ 都可以消灭完后面的一段 ‘)’ ,然后继续消灭后面的 ‘)’ 。
分析完以上的性质后,我们能够确定一个可以一定可以成功的策略,那就是将所有的 ‘(’ 都依次放在前面的’?‘中,这样在此题的条件下,是一定能有一个一定可以匹配成功的策略。
我们分析一下如何找到另一种可能匹配成功的答案,就是让在前后 ‘(’ 和 ‘)’ 数量影响尽可能小的情况下看是是否仍然存在一种可能匹配成功的策略。
如何让两者的数量影响尽可能少?
就是交换离得最近的两个不同的括号也就是本来的’?‘,这样就能保证影响是最小的,因为每一次交换都会影响那一段的左右括号的相对数量,假设一个很远的’)’ 与当前的 ‘(’ 进行交换,这样就会影响的范围会很大,很可能很远的那个’)'就会是最后一个括号。如果这样理解不了的话我们可以这样理解,就是我们每次的最优策略就是左括号尽可能往左放,右括号尽可能往右放。而且我们当前这一步一定是一个左括号和一个右括号换,在这种情况下,我们最优的情况就是拿两个最近的不同的括号,这样就保证了左边的最后一个左括号放到了右边的第一个右括号;右边的第一个右括号放到了左边的最后一个左括号的位置。这种情况一定是当前情况下最优策略。
下面是AC代码:
#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()]);
//判断是否是可以的
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;
}
总结:这道题的解题关键是就是前面的任意一个括号都可以消除后面的一个括号,和之前遇到的括号匹配不相同,所以,这样的题一定是具体问题,具体分析。找出这个题特有的性质,千万不要思维定式。
边栏推荐
- Data analysis and mining 2
- Research Summary / programming FAQs
- Strongly connected component
- Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
- Unity uses NVIDIA flex for unity plug-in to realize the effects of making software, water, fluid, cloth, etc. learning tutorial
- 使用 Fiddler Hook 报错:502 Fiddler - Connection Failed
- The accuracy of yolov7 in cracking down on counterfeits, not all papers are authentic
- Explain the edge cloud in simple terms | 2. architecture
- Mysql库的操作
- 26. Code implementation of file using disk
猜你喜欢

Kotlin类与继承

Meaning of 7 parameters of thread pool

【MATLAB】MATLAB画图系列二 1.元胞与数组转化 2.属性元胞 3.删除nan值 4.合并多fig为同一fig 5.合并多fig至同一axes

Leetcode-09 (next rank + happy number + full rank)
![[300 opencv routines] 238. Harris corner detection in opencv](/img/d7/abdada7cd97060c55861b1bc2a2e13.png)
[300 opencv routines] 238. Harris corner detection in opencv

LeetCode高频题56. 合并区间,将重叠的区间合并为一个区间,包含所有区间

云开发单机版图片九宫格流量主源码

Tiger mouth waterfall: Tongliang version of xiaohukou waterfall

26.文件使用磁盘的代码实现

ISPRS2018/云检测:Cloud/shadow detection based on spectral indices for multi/hyp基于光谱指数的多/高光谱光学遥感成像仪云/影检测
随机推荐
Activate the newly installed Anaconda in the server
Getting started with mongodb
Unity uses NVIDIA flex for unity plug-in to realize the effects of making software, water, fluid, cloth, etc. learning tutorial
Number of bytes occupied by variables of type char short int in memory
dataframe 分组后排序的前n行 nlargest argmax idmax tail !!!!
String application - calculate the longest true prefix of a string
The vs compiled application is missing DLL
新手第一次怎么买股票 哪家证券公司开户最好最安全
Huawei wireless device configuration wpa2-802.1x-aes security policy
Data analysis and mining 1
Date processing bean
Class loading mechanism and parental delegation mechanism
Rest style
Jmmert aggregation test report
Google Earth Engine——使用MODIS数据进行逐月数据的过火(火灾)面积并导出
25. From disk to file
Spark Learning Notes (III) -- basic knowledge of spark core
【MATLAB】MATLAB画图系列二 1.元胞与数组转化 2.属性元胞 3.删除nan值 4.合并多fig为同一fig 5.合并多fig至同一axes
pytorch with torch.no_ grad
Vector introduction and underlying principle