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

运行 ID: 91664

#include <iostream> #include <string> #include <stack> using namespace std; int main() { string str; // 持续读取每行输入,处理多组数据 while (getline(cin, str)) { int n = str.size(); string flag(n, ' '); // 标记串,初始全空格 stack<int> st; for (int i = 0; i < n; ++i) { if (str[i] == '(') { st.push(i); // 左括号下标入栈 } else if (str[i] == ')') { if (!st.empty()) { st.pop(); // 成功匹配,出栈 } else { flag[i] = '?'; // 无匹配的右括号 } } // 字母不作处理,保持空格 } // 处理剩余未匹配的左括号 while (!st.empty()) { int pos = st.top(); st.pop(); flag[pos] = '$'; } // 输出结果 cout << str << endl; cout << flag << endl; } return 0; }