括号匹配问题
1978:扩号匹配问题
输入
((ABCD(x)
)(rttyy())sss)(
输出
((ABCD(x)
$$
)(rttyy())sss)(
? ?$
C++解答
#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;
}
|