P2382 chemical molecular formula

Source: Internet
Author: User
Luogu's oier chemistry must be good.

This question allows us to simulate the process of calculating the chemical equation.

Similar to the time complexity.

We can divide the solution into several steps according to the brackets.

From the external perspective, we need to consider a pair of parentheses as a whole. Then perform the calculation.

From the internal perspective, the subscript outside the brackets has no effect on the internal.

We can think of the given molecular formula in braces.

Then write a function. The function is used to solve the quality in a bracket.

Of course, this function is obviously recursive. Stack is required for recursion. Therefore, the stack is invisible.

# Include <cstdio> # include <algorithm> # include <cstring> # include <iostream> const int maxn = 5000; int T [maxn] [100], tail; int end [maxn]; char C [maxn]; void insert (char * a, int Len, int W) {int now = 0; For (INT I = 1; I <= Len; I ++) {int NXT = A [I]-'A'; If (! T [now] [NXT]) T [now] [NXT] = ++ tail; now = T [now] [NXT];} // trie tree end [now] = W; return;} int get (Int & now, int Len) {int res = 0, R = T [0] [C [now ++]-'a']; while (C [now]> = 'A' & C [now] <= 'Z ') // {int NXT = C [now]-'A'; If (! T [r] [NXT]) Return-0x7fffffff; r = T [r] [NXT]; now ++;} If (! End [R]) Return-0x7fffff; // return the invalid return end [R] without this atom; // return the quality of a single atom} int calc (Int & now, int Len) {int res = 0; while (C [now] <= '9' & C [now]> = '0' & now <= Len) {res = res * 10 + C [now]-'0'; now ++;} // number return res;} int DFS (Int & now, int Len) // {int sum = 0; // The total quality of atoms in parentheses while (C [now]! = ')' & Now <= Len) // No parentheses exist. PS: processing an atom and Its subscript in one loop (if not, it is not processed) {int pas = 0, x = 1; // pas indicates the atomic quality, X is the subscript if (C [now] = '(') // encounters a left brace pas = DFS (++ now, Len ); // recursive processing of else if (C [now] <'0' | C [now]> '9') Pas = get (now, Len ); // calculate the atomic mass if (C [now] = ')' | pas <0) // There is no subscript after it and the right brackets {sum + = PAS; // Add and exit the loop break;} If (C [now]> = '0' & C [now] <= '9') x = calc (now, len); // calculate the subscript sum + = pas * X; // multiply} Now ++; // return sum across the right parenthesis;} int main () {While (true) {int weight; scanf ("% s", C + 1); scanf ("% d", & we Ight); int Len = strlen (C + 1 ); if (C [1] = 'E' & C [2] = 'n' & C [3] = 'D') break; insert (C, len, weight);} // input element while (true) {scanf ("% s", C + 1); If (C [1] = '0 ') break; // termination condition int Len = strlen (C + 1); // length C [Len + 1] = ')'; // extend the brackets of the entire molecular formula, the reason for only covering the right half is that the function I wrote is left open and right closed in the range of the processed parentheses. Int n = 1; int ans = DFS (n, Len + 1); If (ANS <0) printf ("unknown \ n"); // invalid. Else printf ("% d \ n", ANS);} // computing}

Additional: Luxury examples(Of course there may be pot)

P2382 chemical molecular formula

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.