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') &amp;&amp; (s[i]<= 'z'))
	        {
	            ans = ans + s[i];
	        }
	        else if(s[i]=='(')
	        {
	            st.push(s[i]);
	        }
	        else if(s[i]==')')
	        {
	            while(!st.empty() &amp;&amp; st.top()!='(')
	            {
	                char c = st.top();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly