Created
June 21, 2021 17:53
-
-
Save fine-simple/3cc3dff7f116ff72aee6d293e559a3f5 to your computer and use it in GitHub Desktop.
Data Structure Problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int check_precedence(char op) | |
{ | |
switch (op) | |
{ | |
case '+': | |
case '-': | |
return 1; | |
break; | |
case '*': | |
case '/': | |
return 2; | |
break; | |
case '^': | |
return 3; | |
break; | |
} | |
return 0; | |
} | |
string infixToPostfix(string in) | |
{ | |
string out; | |
stack<char> op; | |
for (int i = 0; i < in.size(); i++) | |
{ | |
switch (in[i]) | |
{ | |
case '*': | |
case '/': | |
case '+': | |
case '-': | |
if(op.empty()) | |
op.push(in[i]); | |
else | |
{ | |
if(check_precedence(op.top()) < check_precedence(in[i])) | |
op.push(in[i]); | |
else | |
{ | |
out += op.top(); | |
op.pop(); | |
op.push(in[i]); | |
} | |
} | |
break; | |
case '(': | |
op.push(in[i]); | |
break; | |
case ')': | |
while(op.top() != '(') | |
{ | |
out += op.top(); | |
op.pop(); | |
} | |
op.pop(); | |
break; | |
default: | |
out += in[i]; | |
break; | |
} | |
} | |
while (!op.empty()) | |
{ | |
out+= op.top(); | |
op.pop(); | |
} | |
return out; | |
} | |
int main() | |
{ | |
string in; | |
cin >> in; | |
cout << infixToPostfix(in) << '\n'; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int main() | |
{ | |
vector<int> seq; | |
bool dec=0, valid = 1; | |
int n; | |
cin >> n; | |
int tmp; | |
cin >> tmp; | |
seq.push_back(tmp); | |
n--; | |
while (n--) | |
{ | |
cin >> tmp; | |
if(dec == 0) | |
{ | |
if(seq[seq.size() -1] < tmp) | |
seq.push_back(tmp); | |
else | |
dec = 1; | |
} | |
if(dec == 1) | |
{ | |
if(seq[seq.size() -1] > tmp && find(seq.begin(), seq.end(), tmp) != seq.end()) | |
{ | |
seq.push_back(tmp); | |
} | |
else | |
{ | |
valid = 0; | |
break; | |
} | |
} | |
} | |
if(valid) | |
cout << "Yes\n"; | |
else | |
cout << "No\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment