Demo_B11 @ 2024-06-09 16:14:57
此题Code:
#include<bits/stdc++.h>
using namespace std;
string s;
bool vis[110];
inline char Hou(char c){
if(c=='(')return ')';
if(c=='[')return ']';
}
inline char Qian(char c){
if(c==')')return '(';
if(c==']')return '[';
}
inline bool Is(char c){
return(c=='(' || c=='[' );
}
struct c{
char c;
int id;
}w[110],t;
stack<c> S;
int main(){
for(int i=1;i<=105;++i){
vis[i]=1;
}
cin>>s;
for(int i=1;i<=s.size();++i){
w[i].c=s[i-1];
w[i].id=i;
}
for(int i=1;i<=s.size();++i){
if(S.empty()){
S.push(w[i]);
}
else{
t=S.top();
if(Is(t.c) && Hou(t.c)==w[i].c){
vis[t.id]=vis[i]=0;
S.pop();
}else{
S.push(w[i]);
}
}
}
for(int i=1;i<=s.size();++i){
if(!vis[i]){putchar(w[i].c);}
else{
if(Is(w[i].c))
putchar(w[i].c),putchar(Hou(w[i].c));
else
putchar(Qian(w[i].c)),putchar(w[i].c);
}
}
return 0;
}
by 123huchenghao @ 2024-06-28 20:40:18
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> stk;
string str;
cin >> str;
int len = str.length();
int f[101] = { 0 };
for (int i = 0; i < len; i++)
{
if (str[i] == ')')
{
if (!stk.empty() && str[stk.top()] == '(')
{
f[stk.top()] = f[i] = 1;
stk.pop();
}
}
else if (str[i] == ']')
{
if (!stk.empty() && str[stk.top()] == '[')
{
f[stk.top()] = f[i] = 1;
stk.pop();
}
}
else stk.push(i);
}
for (int i = 0; i < len; i++)
{
if (f[i])
{
cout << str[i];
}
else
{
if (str[i] == ']' || str[i] == '[')cout << "[]";
if (str[i] == ')' || str[i] == '(')cout << "()";
}
}
return 0;
}