做法1:闭门造车
#include <iostream> #include <cstdio> #include <string> #include <stack> using namespace std; const int MAXSIZE=100; stack<int> Left; int main() { string str; while(cin>>str) { int arr[MAXSIZE]= {0}; for(int i=0; i<str.size(); ++i) { if(str[i]=='(') { Left.push(i); } if(str[i]==')') { if(!Left.empty()) { Left.pop(); } else { arr[i]=1; } } } while(!Left.empty()) { arr[Left.top()]=-1; Left.pop(); } cout<<str<<endl; for(int i=0; i<str.size(); ++i) { if(arr[i]==1) { cout<<'?'; } else if(arr[i]==-1) { cout<<'$'; } else { cout<<' '; } } cout<<endl; } }
做法2:作者代码
作者链接
https://github.com/BenedictYoung/Lecture/blob/master/Chapter5/5.5.cpp
/* * 题目名称:扩号匹配问题 * 题目来源:OpenJudge 1978 * 题目链接:http://ccnu.openjudge.cn/practice/1978/ * 代码作者:杨泽邦(炉灰) */ #include <iostream> #include <cstdio> #include <string> #include <stack> using namespace std; int main() { string str; while (cin >> str) { stack<int> brackets; string answer(str.size(), ' '); //初始化为输入长度个空格 for (int i = 0; i < str.size(); ++i) { if (str[i] == '(') { brackets.push(i); //压入左括号下标 } else if (str[i] == ')') { if (!brackets.empty()) { brackets.pop(); } else { answer[i] = '?'; //右括号不匹配 } } } while (!brackets.empty()) { answer[brackets.top()] = '$'; //左括号不匹配 brackets.pop(); } cout << str << endl; cout << answer << endl; } return 0; }