这题其实不用递归,直接用stack就行!
//扩号匹配问题
#include <iostream>
#include <stack>
using namespace std;
int main() {
char s[110];
while (cin >> s) {
int len = strlen(s);
stack<int> ch;
bool b[110] = {0};
for (int i = 0; i < len; i ++) {
if (s[i] == '(')
ch.push(i);
if (s[i] == ')') {
if (!ch.empty())
ch.pop();
else
b[i] = true;
}
}
while (!ch.empty()) {
b[ch.top()] = true;
ch.pop();
}
cout << s << '\n';
for (int i = 0; i < len; i ++)
cout << (b[i] ? (s[i] == '(' ? '$' : '?') : ' ');
cout << '\n';
}
return 0;
}
/*
【输入样例】
((ABCD(x)
)(rttyy())sss)(
【输出样例】
((ABCD(x)
$$
)(rttyy())sss)(
? ?$
*/
定义一个b数组记录 左/右括号能不能匹配
这题就是一本通1353的升级版!
想看看一本通1353题解?:一本通1353_小王子009的博客-CSDN博客
一本通1353:信息学奥赛一本通(C++版)在线评测系统
一本通1203:信息学奥赛一本通(C++版)在线评测系统