社区讨论
求助求助求助!!!
AT_abc307_d [ABC307D] Mismatched Parentheses参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @lo18qw90
- 此快照首次捕获于
- 2023/10/22 17:03 2 年前
- 此快照最后确认于
- 2023/11/02 16:52 2 年前
不知道为什么WA了10几个点,只A了34个点
后面写的比较冗余,主要是为了倒序输出
CPP#include<bits/stdc++.h>
using namespace std;
int n,ans[2000005];
string s;
struct node{
char str;
int id;
};
stack<node>a;
stack<node>b;
int main()
{
ios::sync_with_stdio(0);
cin>>n>>s;
for(int i=0;i<s.length();i++)
{
if(s[i]>='a' && s[i]<='z')a.push((node){s[i],i});
else
{
if(s[i]=='(')b.push((node){s[i],i});
else
{
if(b.empty())b.push((node){s[i],i});
else if(b.top().str!='(')b.push((node){s[i],i});
else
{
if(!a.empty())a.pop();
b.pop();
}
}
}
}
int cnt=1;
while(!a.empty())
{
ans[cnt++]=a.top().id;
a.pop();
}
while(!b.empty())
{
ans[cnt++]=b.top().id;
b.pop();
}
sort(ans+1,ans+cnt);
for(int i=1;i<=cnt-1;i++)cout<<s[ans[i]];
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...