78pts + #1,2,3WA求助
查看原帖
78pts + #1,2,3WA求助
191248
Genshineer楼主2021/12/14 21:10
#include <bits/stdc++.h>
using namespace std;
char s[105];
stack<int> s1, s2;
bool b[105];
int main( ) {
	memset(b, 0, sizeof(b));
	cin >> s;
	for(int i = strlen(s) - 1; i >= 0; i--) {
		if(s[i] == ']') {
			s2.push(i);
//			cout << "push2" << endl;
		}
		if(s[i] == ')') {
			s1.push(i);
//			cout << "push1" << endl;
		}
		if(1) {
			if(s[i] == '(') {
//				cout << "go with(" << endl;
				if(!s1.empty() || !s2.empty()){
					while(!s1.empty() && !s2.empty() && s2.top() > i && s2.top() < s1.top()) {
						s2.pop();
					}
					if(!s1.empty()) {
						b[i] = 1;
						b[s1.top()] = 1;
//						cout << i << " " << s1.top() << endl;
						s1.pop();
					}
				}
			}
			if(s[i] == '[') {
//				cout << "go with[" << endl;
				if(!s1.empty() || !s2.empty()) {
					while(!s1.empty() && !s2.empty() && s1.top() > i && s1.top() < s2.top()) {
						s1.pop();
					}
					if(!s2.empty()) {
						b[i] = 1;
						b[s2.top()] = 1;
//						cout << i << " " << s2.top() << endl;
						s2.pop();
					}
				}
			}
		}
//		cout << s[i] << endl;
	}
	for(int i = 0; i < strlen(s); i++) {
		if(b[i] != 1) {
			if(s[i] == '(' || s[i] == ')') {
				cout << "()";
			}
			else {
				cout << "[]";
			}
		}
		else {
			cout << s[i];
		}
	}
/*	cout << endl;
	for(int i = 0; i < s.size(); i++) {
		cout << b[i] << " ";
	}*/
} 

我好菜\color{white}{\text{我好菜}}

2021/12/14 21:10
加载中...