Stacks Flashcards
1
Q
Infix to postfix (reverse polish notation) O(n)
A
define ll long long
#include using namespace std;
int precedence(char c) { if(c=='^') return 5; if(c=='/') return 4; if(c=='*') return 3; if(c=='-') return 2; //for + return 1; }
int main() { ll t; cin>>t; while(t--) { string s,ans; cin>>s; ll n,i; n = s.length(); stack st; for(i=0;i= 'a') && (s[i]<= 'z')) { ans = ans + s[i]; } else if(s[i]=='(') { st.push(s[i]); } else if(s[i]==')') { while(!st.empty() && st.top()!='(') { char c = st.top();
2
Q
Parenthesis checker O(n)
A
define ll long long
#include using namespace std;
int main() { ll t; cin>>t; while(t--) { string s; cin>>s; ll n,i; n = s.length(); stack st; st.push(s[0]); i=1; while(i