Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using ld = long double;
- using ull = unsigned long long;
- bool bad = false;
- bool isdig(char x) {
- return x == '0' || x == '1';
- }
- ll parseE(const string& s);
- ll parseN(const string& s) {
- if (s.empty()) {
- bad = true;
- return 0;
- }
- if (s[0] == '0') {
- if (s.size() != 1) {
- bad = true;
- }
- return 0;
- }
- ll ans = 0;
- for (char c : s) {
- if (!isdig(c)) {
- bad = true;
- return 0;
- }
- ans = 2 * ans + c - '0';
- }
- return ans;
- }
- ll parseF(const string& s) {
- if (s.empty()) {
- bad = true;
- return 0;
- }
- if (s[0] == '-') {
- return -parseF(s.substr(1, s.size()));
- } else if (s[0] == '(') {
- if (s.back() != ')') {
- bad = true;
- return 0;
- }
- return parseE(s.substr(1, s.size() - 2));
- } else {
- return parseN(s);
- }
- }
- ll parseT(const string& s) {
- // cerr << s << endl;
- if (s.empty()) {
- bad = true;
- return 0;
- }
- int bal = 0;
- for (int i = s.length() - 1; i >= 0; i--){
- if (s[i] == ')') bal++;
- if (s[i] == '(') bal--;
- if (s[i] == '*' && bal == 0) {
- ll lhs = parseT(s.substr(0, i)), rhs = parseF(s.substr(i + 1, s.size()));
- return lhs * rhs;
- }
- }
- return parseF(s);
- }
- ll parseE(const string& s) {
- if (s.empty()) {
- bad = true;
- return 0;
- }
- int bal = 0;
- for (int i = s.length() - 1; i >= 0; i--) {
- if (s[i] == ')') {
- bal++;
- } else if (s[i] == '(') {
- bal--;
- } else if (bal == 0) {
- if (s[i] == '+' || s[i] == '-') {
- if (i != 0 && i + 1 != s.length()) {
- if (s[i - 1] == '(') continue;
- if (s[i - 1] == '+') continue;
- if (s[i - 1] == '-') continue;
- if (s[i - 1] == '*') continue;
- ll lhs = parseE(s.substr(0, i));
- ll rhs = parseT(s.substr(i + 1, s.size()));
- if (s[i] == '+') {
- return lhs + rhs;
- } else {
- return lhs - rhs;
- }
- }
- }
- }
- if (bal < 0) {
- bad = true;
- return 0;
- }
- }
- if (bal != 0) {
- bad = true;
- return 0;
- }
- return parseT(s);
- }
- bool check(const string& expr) {
- vector<int> pos;
- for (int i = 0; i < expr.size(); i++) {
- if (expr[i] == '=') {
- pos.push_back(i);
- }
- }
- if (pos.size() != 1) {
- return false;
- }
- string lhs = expr.substr(0, pos[0]);
- string rhs = expr.substr(pos[0] + 1, expr.size());
- if (lhs.empty() || rhs.empty()) {
- return false;
- }
- bad = false;
- bool ok = (parseE(lhs) == parseE(rhs));
- if (bad) {
- return false;
- }
- return ok;
- }
- int main() {
- #ifdef NIZNIY_MAGAZIN_MIR_V_CENTRE_VINNITSY
- freopen("in", "r", stdin);
- #endif
- ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- string s;
- cin >> s;
- vector<char> have;
- for (char c : s) {
- if (isupper(c) || islower(c)) {
- have.push_back(c);
- }
- }
- sort(have.begin(), have.end());
- have.resize(unique(have.begin(), have.end()) - have.begin());
- vector<char> p = {'0', '1', '+', '-', '*', '(', ')', '='};
- sort(p.begin(), p.end());
- set<string> go;
- do {
- string expr;
- for (int i = 0; i < s.size(); i++) {
- if (isupper(s[i]) || islower(s[i])) {
- int pos = 0;
- while (have[pos] != s[i]) pos++;
- expr += p[pos];
- } else {
- expr += s[i];
- }
- }
- // assert(expr != "-0=0");
- if (check(expr)) {
- go.insert(expr);
- }
- } while (next_permutation(p.begin(), p.end()));
- cout << go.size() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement