MAXimal
home
algo
bookz
forum
about
������ ���������. �������� �������� �������
Page source on a
TeX
-like language:
\h1{������ ���������. �������� �������� �������} ���� ������, �������������� ����� �������������� ���������, ���������� �����, ����������, ��������� ��������. ��������� ��������� ��� �������� �� $O (n)$, ��� $n$ --- ����� ������. ����� ������ ��������, ������� ��������� ��� ��������� � ��� ���������� \bf{�������� �������� �������} (����� ��� ������� �������), � ��� � ��� ��������� ���������. \h2{�������� �������� �������} �������� �������� ������� --- ��� ����� ������ �������������� ���������, � ������� ��������� ����������� ����� ����� ���������. ��������, ��������� ���������: $$ a + b * c * d + (e - f) * (g * h + i) $$ � �������� �������� ������� ������������ ��������� �������: $$ a b c * d * + e f - g h * i + * + $$ �������� �������� ������� ���� ����������� ������������� ��������� � ������������ � ������� ������ �������������� ����� �������� ��������� � �������� 1950-� �� ������ �������� �������, ������� ���� ���������� � 1920 �. �������� ����������� ���� �����������. �������� �������� �������� ������� ����������� � ���, ��� ���������, �������������� � ����� �����, ����� \bf{����� ���������}, ������ �� �������� �����. ������ ����, ���������� �� ����. ����� ��������� ����� ������� �� ��������� � �������� �������� �������; ���� ������� ������� --- ����� ��� ����������, �� ����� �� ������� ����� � ��������; ���� �� ������� ������� --- ��������, �� ������ �� ����� ��� ������� �������� (��� ����, ���� �������� �������), ��������� � ��� ��������, � ��������� ����� ������� � ����. � ����� ������ � ����� ��������� ����� ���� ������� - �������� ���������. ��������, ���� ������� �������� ����������� �� $O (n)$, �.�. ������� ����� ���������. \h2{������ ���������� ���������} ���� �� ������������� ������ ���������� ������: ��� �������� \bf{�������} (�.�. �� ���� ����������), � ��� \bf{����������������} (�.�. ��� ��������� ����������� ����������� ����� �������). ������ ���������. ������ ��� �����: ���� ��� �����, ������ ��� �������� � ������ (�.�. ���� ��������). ���������� ��� ����� �����. ��� ������� ����� ����� ������������ �����������, ��� ��� �������� ����������� � ��� �� �������� �������� ����������, ���� ��������� �� ������� �����. ���� � ����� ���� ����������� ������, �� ���������� ������ ���� ��������, ����������� ����� ��������, � ���� ���� � ����� ������ �� ����������� ����������. ����� ���� �� ������ ����� �������. ���� ������� ������� --- ����� ��� ����������, �� ������� � ���� �������� ����� �����/����������. ���� ������� ������� --- ����������� ������, �� ������� � � ����. ���� ������� ������� --- ����������� ������, �� ����� ����������� �� ����� � ��������� ��� �������� �� ��� ���, ���� �� �� �������� ����������� ������ (�.�., ����� ������, �������� ����������� ������, �� ��������� ��� ��������, ����������� ������ ���� ������). �������, ���� ������� ������� --- ��������, ��, ���� �� ������� ����� ��������� �������� � ����� �� ��� ������� �����������, ����� ����������� � ��������� �. ����� ����, ��� �� ���������� ��� ������, � ����� �������� ��� ����� �������� ��������� ��������, ������� ��� �� ���� ���������, � ����� ��������� �� ��� (�.�. ��������� ���������� ������, ����� ��������� ����������� ������). ��� ���������� ������� ������ �� ������� ������� �������� $+-*/\%$: \code bool delim (char c) { return c == ' '; } bool is_op (char c) { return c=='+' || c=='-' || c=='*' || c=='/' || c=='%'; } int priority (char op) { return op == '+' || op == '-' ? 1 : op == '*' || op == '/' || op == '%' ? 2 : -1; } void process_op (vector
& st, char op) { int r = st.back(); st.pop_back(); int l = st.back(); st.pop_back(); switch (op) { case '+': st.push_back (l + r); break; case '-': st.push_back (l - r); break; case '*': st.push_back (l * r); break; case '/': st.push_back (l / r); break; case '%': st.push_back (l % r); break; } } int calc (string & s) { vector
st; vector
op; for (size_t i=0; i
= priority(s[i])) process_op (st, op.back()), op.pop_back(); op.push_back (curop); } else { string operand; while (i < s.length() && isalnum (s[i]))) operand += s[i++]; --i; if (isdigit (operand[0])) st.push_back (atoi (operand.c_str())); else st.push_back (get_variable_val (operand)); } while (!op.empty()) process_op (st, op.back()), op.pop_back(); return st.back(); } \endcode ����� �������, �� ��������� ��������� �������� ��������� �� $O (n)$, � ��� ���� �� ������ ��������������� �������� �������� ��������: �� ����������� �������� � ����� �������, ����� � ������� ���������� ��������� �������� ��� � �������� ��� ���������. ������ ������������� ������������� ��������, ����� �������� ��������� � �������� �������� ������ � � ����� ����. \h2{������� ��������} ������ �����������, ��� ��������� �������� ������� �������� (�.�. �� ������ ���������). ��������, �������� ����� ����������� ������� ���� � �����. ���� �� ������� ����� ������ ����������� � ������������� ����������� ����, �������� �� ������� �������� ������� ��� ��������. ����� ��������, ��� ����� ������� ��������� ������ ����� ���� ������ ��������, ���� ����������� ������, ���� ������ ������ (���� ��� ����� � ����� ������ ������). ����� �������� ���������, ��������, ������ ����� ���� ������� (�����/����������), ���� ����������� ������. ����� �������, ���������� ������� �����-������ ���� ��� �������� ����, ����� �� ��������� �������� ���� ������� ��� ���. ��� ����� �������������� �������� --- ��� ��������� ������� � �������� �������� ��� ���������� �� ����� � ����������. ����� �����, ��������, ��� ������� �������� ������ ������� $s[i]$ ������ � ���� $-s[i]$. ��������� ��� ������� �������� ����� �������� �����, ����� �� ��� ������ ����������� ���� �������� ��������. ����� ����, ���� ��������, ��� ������� �������� ���������� �������� ������������������� --- ���� ������ ���� ��������� ������� ��������, �� ��� ������ �������������� ������ ������ (��� �������� ����� ������ ��. ����; ���������� ����� ��� ��� ��������� ��������������������). ���������� ��� �������� �������� $+-*/$ � ������� �������� $+-$: \code bool delim (char c) { return c == ' '; } bool is_op (char c) { return c=='+' || c=='-' || c=='*' || c=='/' || c=='%'; } int priority (char op) { if (op < 0) return 4; // op == -'+' || op == -'-' return op == '+' || op == '-' ? 1 : op == '*' || op == '/' || op == '%' ? 2 : -1; } void process_op (vector
& st, char op) { if (op < 0) { int l = st.back(); st.pop_back(); switch (-op) { case '+': st.push_back (l); break; case '-': st.push_back (-l); break; } } else { int r = st.back(); st.pop_back(); int l = st.back(); st.pop_back(); switch (op) { case '+': st.push_back (l + r); break; case '-': st.push_back (l - r); break; case '*': st.push_back (l * r); break; case '/': st.push_back (l / r); break; case '%': st.push_back (l % r); break; } } } int calc (string & s) { bool may_unary = true; vector
st; vector
op; for (size_t i=0; i
= 0 && priority(op.back()) >= priority(curop) || curop < 0 && priority(op.back()) > priority(curop)) ) process_op (st, op.back()), op.pop_back(); op.push_back (curop); may_unary = true; } else { string operand; while (i < s.length() && isalnum (s[i]))) operand += s[i++]; --i; if (isdigit (operand[0])) st.push_back (atoi (operand.c_str())); else st.push_back (get_variable_val (operand)); may_unary = false; } while (!op.empty()) process_op (st, op.back()), op.pop_back(); return st.back(); } \endcode ����� ��������, ��� � ���������� �������, ��������, ����� �� ������� �������� ��������� ������ $+$ � $-$, �������������������� �� ������ ������� ����, ������� � ����� ��������� ������� ���������� � ����� ����� �� �������. �.�. ����: \code while (!op.empty() && ( curop >= 0 && priority(op.back()) >= priority(curop) || curop < 0 && priority(op.back()) > priority(curop)) ) process_op (st, op.back()), op.pop_back(); \endcode ����� �������� ��: \code while (!op.empty() && priority(op.back()) >= priority(curop)) process_op (st, op.back()), op.pop_back(); \endcode \h2{��������������������} �������������������� ��������� ��������, ��� ��� ��������� ����������� ��������� ����������� ������ ������ (�������������, ������������������� - ����� ����� �������). ��� ��� ���� �������� ����, ������� ��������� ������ �������� �������������������. ������ ������ - ������ �������� ���������� � ������� ��������� ������������������ (�������������, a^b^c ������ �������������� ��� a^(b^c), � �� (a^b)^c). ����� ������� ����� ������ � ��������, ����� ��������� ������������ ��������������������? �� ����� ����, ��������� ����� ����� �����������. ������������ ������� ����� ����������� ������ ��� ��������� �����������, � ����������� ��� � ���, ��� �������� � ������ �����������, ����������� �� ������� �����, �� ������ ��������� ������ ������� ��������. ����� �������, ������������ ������� ����� ������ � ������� calc: \code int calc (string & s) { ... while (!op.empty() && ( left_assoc(curop) && priority(op.back()) >= priority(curop) || !left_assoc(curop) && priority(op.back()) > priority(curop))) ... } \endcode