problem:
一个表达式中括号表示将里面的字符串翻转。
输入:一行字符串
输出:一行字符串,如果表达式符号不匹配输出空字符串。
solution:
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#include <string>
int main()
{
string a;
cin>>a;
stack<char> st;
queue<char> qu;
string aa;
for(auto b:a)
{
while(!qu.empty())
{
st.push(qu.front());
qu.pop();
}
if(b!=')')//如果不是右括号就入栈
st.push(b);
else
{
while(st.top()!='(')
{
qu.push(st.top());
st.pop();
}
if(st.top()=='(') st.pop();
else
{
cout<<""<<endl;
return 0;
}
}
}
if(!st.empty())
{
cout<<"";
return 0;
}
while(!qu.empty())
{
aa+=qu.front();
qu.pop();
}
cout<<aa<<endl;
}