+ upd
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <stack>
|
||||
|
||||
void optimize(std::map<char, std::vector<std::string>>& map) {
|
||||
std::set<char> exist;
|
||||
std::stack<char> stack;
|
||||
|
||||
|
||||
stack.push('S');
|
||||
exist.insert('S');
|
||||
|
||||
while (!stack.empty()) {
|
||||
char now = stack.top();
|
||||
stack.pop();
|
||||
|
||||
for (const auto& str : map[now]) {
|
||||
for (const auto& c : str) {
|
||||
if (!('A' <= c && c <= 'Z')) {
|
||||
continue;
|
||||
}
|
||||
if (exist.find(c) == exist.end()) {
|
||||
exist.insert(c);
|
||||
stack.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto it = map.begin();
|
||||
while (it != map.end()) {
|
||||
if (exist.find(it->first) == exist.end()) {
|
||||
it = map.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// std::vector<std::pair<char, std::string>> mas;
|
||||
std::map<char, std::vector<std::string>> map;
|
||||
char left;
|
||||
std::string right;
|
||||
|
||||
while (std::cin >> left >> right) {
|
||||
// mas.push_back(std::make_pair(left, right));
|
||||
map[left].push_back(right);
|
||||
}
|
||||
|
||||
optimize(map);
|
||||
|
||||
// for (const auto& rule : mas) {
|
||||
// if (map.find(rule.first) != map.end()) {
|
||||
// std::cout << rule.first << " " << rule.second << std::endl;
|
||||
// }
|
||||
// }
|
||||
|
||||
for (const auto& rules : map) {
|
||||
for (const auto& rule : rules.second) {
|
||||
std::cout << rules.first << " " << rule << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
На стандартный поток ввода программе подаётся выражение в польской записи. Выражение может содержать имена переменных и бинарные операции +, -, *, /. Имя переменной — это строчная латинская буква. Элементы ввода могут разделяться произвольным количеством пробельных символов.
|
||||
|
||||
Напечатайте введённое выражение в инфиксной записи. Для сохранения приоритетов бинарные операции заключите в скобки. Выражение напечатайте в одной строке текста без пробельных символов.
|
||||
Examples
|
||||
Input
|
||||
|
||||
abc++
|
||||
|
||||
Output
|
||||
|
||||
(a+(b+c))
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stack>
|
||||
|
||||
bool is_operation(char c) {
|
||||
return c == '+' || c == '-' || c == '*' || c == '/';
|
||||
}
|
||||
|
||||
int main() {
|
||||
char c;
|
||||
std::stack<std::string> stack;
|
||||
|
||||
while (std::cin >> c) {
|
||||
if (is_operation(c)) {
|
||||
std::string r = stack.top();
|
||||
stack.pop();
|
||||
|
||||
std::string l = stack.top();
|
||||
stack.pop();
|
||||
|
||||
stack.push("(" + l + c + r + ")");
|
||||
} else {
|
||||
stack.push(std::string(1, c));
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << stack.top() << std::endl;
|
||||
|
||||
stack.pop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user