24求助悬关

P1241 括号序列

@[small_Dongpo](/user/741732) 给个样例吧 比如 >( [ ) ] ) 输出的应该是 >( [ ( ) ] ) 而不是 >( ) [ ] ( ) [ ] ( )
by sss12 @ 2024-03-22 12:55:24


现在的代码: ```cpp #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <stack> using namespace std; typedef long long l; string s, ans = ""; stack<char> st; int main() { cin >> s; for (l i = 0; i < s.size(); ++i) { if (s[i] == '(') { st.push('('); ans.push_back('('); } else if (s[i] == '[') { st.push('['); ans.push_back('['); } else if (s[i] == ')') { if (!st.empty() && st.top() == '(') { st.pop(); ans.push_back(')'); } else { ans.push_back('('); ans.push_back(')'); } } else if (s[i] == ']') { if (!st.empty() && st.top() == '[') { st.pop(); ans.push_back(']'); } else { ans.push_back('['); ans.push_back(']'); } } } while (!st.empty()) { if (st.top() == '(') { ans.push_back(')'); } else { ans.push_back(']'); } st.pop(); } cout << ans; return 0; } ``` 60pts
by small_Dongpo @ 2024-03-22 21:56:16


|