-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPOJ-ONP.cpp
More file actions
63 lines (57 loc) · 1.27 KB
/
SPOJ-ONP.cpp
File metadata and controls
63 lines (57 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;
int const N = 1e6 + 1;
int n, a[N], cs[N];
int procura(string s, char c){
int i;
int tam = s.length();
for(i = 0; i < tam; i++){
if(s[i] == c) return i;
}
return -1;
}
int main() {
int n;
cin >> n;
for(int i = 0; i < n; i++){
stack<char> pilha;
queue<char> fila;
string s;
cin >> s;
int tam = s.length();
string ops ("+-*/^");
for(int j = 0; j < tam; j++){
if(isalpha(s[j])){
fila.push(s[j]);
}else if(s[j] == '('){
pilha.push(s[j]);
}else if(s[j] == ')'){
while(pilha.top() != '('){
fila.push(pilha.top());
pilha.pop();
}
pilha.pop();
}else{
//cout << s[j] << " " << pilha.top() << endl;
//cout << procura(ops, s[j]) << " " << procura(ops, pilha.top()) << endl;
if(procura(ops, s[j]) < procura(ops, pilha.top())){
fila.push(pilha.top());
pilha.pop();
pilha.push(s[j]);
}else{
//cout << "to entrando nesse carai " << s[j] << endl;
pilha.push(s[j]);
}
}
//cout << "Para " << s[j] << ":\n";
//if(!fila.empty()) cout << "Fila: " << fila.back() << endl;
//if(!pilha.empty()) cout << "Pilha: " <<pilha.top() << endl << endl;
}
while(!fila.empty()){
cout << fila.front();
fila.pop();
}
cout << endl;
}
return 0;
}