提交时间:2026-06-14 14:19:56

运行 ID: 91663

#include <iostream> #include <string> #include <stack> using namespace std; int main() { string s; // 多组输入,逐行读取 while (getline(cin, s)) { int len = s.size(); string res(len, ' '); // 初始全为空格 stack<int> st; for (int i = 0; i < len; ++i) { if (s[i] == '(') { st.push(i); } else if (s[i] == ')') { if (!st.empty()) { st.pop(); // 匹配成功,不标记 } else { res[i] = '?'; // 右括号失配 } } // 字母直接忽略,保持空格 } // 处理剩余未匹配的左括号 while (!st.empty()) { int pos = st.top(); st.pop(); res[pos] = '$'; } // 输出结果 cout << s << endl; cout << res << endl; } return 0; }