string插入一个字符s.insert((string::size_type)0,1,'a');
先由stack的后缀表达式得到原表达式的树,再由queue得到后缀表达式
#include<iostream> #include<stack> #include<queue> #include<cctype> #include <algorithm> #include<string> #include<cstring> using namespace std; struct node { char ch; int left,right,oder; }; int main() { int sum; cin>>sum; cin.ignore(); while(sum--) { string temp,ss=""; getline(cin,temp); int i; stack<node>st; node tree[10010]; for(i=0;i<temp.length();i++) { node no; no.oder=i; if(islower(temp[i])) { no.ch=temp[i]; no.left=-1; no.right=-1; st.push(no); } else { node y,x; y=st.top(); st.pop(); x=st.top(); st.pop(); no.ch=temp[i]; no.left=x.oder; no.right=y.oder; st.push(no); tree[x.oder]=x; tree[y.oder]=y; } } queue<node>qu; qu.push(st.top()); node temp_no; while(!qu.empty()) { temp_no=qu.front(); qu.pop(); ss+=temp_no.ch; if(temp_no.left!=-1) qu.push(tree[temp_no.left]); if(temp_no.right!=-1) qu.push(tree[temp_no.right]); } reverse(ss.begin(),ss.end()); cout<<ss<<endl; } return 0; }