68分怎么办??
查看原帖
68分怎么办??
1010999
FanHangrui楼主2024/9/18 18:46

###P1241 68分怎么搞?

###求大佬援助!!

###代码如下:

#include<bits/stdc++.h>
using namespace std;
stack<int> st;
bool ok[105];
int main(int argc, const char *argv[]) {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	string s;
	cin >> s;
	int len = s.length();
	for(int i = 0, k; i < len; i ++) {
		if (s[i] == ']') {
			if(st.empty()) continue;
			k = st.top();
			if (s[k] == '[') {
				ok[k] = ok[i] = 1;
				st.pop();
			}
		} else if(s[i] == ')') {
			if(st.empty()) continue;
			k = st.top();
			if (s[k] == '(') {
				ok[k] = ok[i] = 1;
				st.pop();
			}
		} else {
			st.push(i);
		}	
	}
	for(int i = 0; i < len; i ++) {
		if(ok[i]) cout << s[i];
		else {
			if (s[i] == '(' || s[i] == ')') printf("()");
			else printf("[]");
		}
	}
	return 0;
}

###求指导!!

2024/9/18 18:46
加载中...