aeknights @ 2024-01-05 12:50:47
测试点8,9,10,11WA
#include<bits/stdc++.h>
using namespace std;
string s,s1;
char str[110];
int site[110];
int main(){
cin>>s;
int leng = s.size(),top = 0;
bool IsRight = false;
for(int i = 0;i<leng;i++){
char a = s[i];
if(a == '['){
top++;
str[top] = a;
site[i] = 1;
}
if(a == '('){
top++;
str[top] = a;
site[i] = 1;
}
if(a == ']'){
if(str[top] == '['){
top--;
site[i-1] = 0;
}else{
IsRight = false;
}
}
if(a == ')'){
if(str[top] == '('){
top--;
site[i-1] = 0;
}else{
IsRight = false;
}
}
}
if(top == 0) IsRight = true;
if(s[0] == ')') IsRight = false;
if(s[0] == ']') IsRight = false;
if(IsRight) cout<<s;
else{
for(int i = 0;i<leng;i++){
if(s[i] == '['){
cout<<s[i];
cout<<"]";
}
if(s[i] == '('){
cout<<s[i];
cout<<")";
}
if(s[i] == ']'){
cout<<"[";
cout<<s[i];
}
if(s[i] == ')'){
cout<<"(";
cout<<s[i];
}
}
}
return 0;
}
by WiliestJoseph15 @ 2024-01-17 10:46:28
if(a == ']'){
if(str[top] == '['){
top--;
site[i-1] = 0;
}else{
IsRight = false;
}
配对的括号并不是一定挨在一起的,如(])这样,所以不能直接标记右括号之前的那一位。可以试试把左括号的下标压入栈,配对的时候再通过下标在字符串中找出来验证