Here is the problem link : INPSTFIX
And my code is below :
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define desc greater<int>()
#define all(a) a.begin(), a.end()
#define PI 2*acos(0)
#define mem(a, x) memset(a, x, sizeof(a))
#define TEZ ios_base::sync_with_stdio(0); \
cin.tie(0); cout.tie(0); \
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int prior(char ch) {
if(ch == '+' || ch == '-') return 1;
if(ch == '*' || ch == '/') return 2;
if(ch == '^') return 3;
}
void solve() {
int n; cin >> n;
string s; cin >> s;
s = '(' + s + ')';
n += 2;
stack<char> knap;
string result = "";
for(int i=0; i<n; i++) {
if(s[i] == '(') knap.push(s[i]);
else if(s[i] == ')') {
while(knap.top() != '(') {
result += knap.top();
knap.pop();
}
knap.pop();
} else if(s[i] >= 'A' && s[i] <= 'Z') {
result += s[i];
} else {
while(knap.top() != '(' && prior(knap.top()) > prior(s[i])) {
result += knap.top();
knap.pop();
}
knap.push(s[i]);
}
}
cout << result;
cout << '\n';
}
int main() {
TEZ;
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
// TEST CASES
int tc = 1; cin >> tc;
while(tc--) solve();
return 0;
}
I am getting WA and I don’t know why because code looks better for me. Any help would be appreciated!