Luogu P2382 Chemical formula (analogue)

Source: Internet
Author: User

Topic topic Background

The Fuhrer and the Senate are working together to learn chemistry, and they want computers to help him simulate the burden of molecular formula. Please help him design a program.

Title Description

Your task is to write a program that can handle the molecular formula in a virtual chemistry in which each molecule is described as one or more atoms, but it may not have a real chemical.

The following are definitions of atomic symbols and molecular formula:

An atom in a molecule is represented by an atomic symbol, and the atomic symbol consists of a single capital letter or an uppercase and lowercase letter. For example: H and he are atomic symbols.

A formula is a non-empty sequence of an atomic symbol, for example,\ (hhhehhhe\) is a molecular formula that represents a molecule consisting of \ (4\) ( h\) and \ (2\) ( He \).

For convenience, a section of the same equation, such as \ (x\cdots x\) (\ (n\) ( x\),\ (2\leq n\leq 99\), can be abbreviated as \ ((x) _n\). If \ (x\) is an atomic symbol, then the parentheses can be omitted. For example,\ (hhhehhhe\) can also write \ (h_2heh_2he\),\ ((hhhe) _2\),\ ((h_2he) _2\), \ (((H) _ 2He) (_2\).

The definition of a formula can be described in a canonical language. In short, the syntax of the formula is described as follows:

Molecule: Atom | atomic number | (molecule) number | molecule molecule
Atom: Uppercase Letter | capital Letter Lowercase Letter

Number:\ (2|3|4|5|\cdots|99|\)
Capital letters:\ (a| b|\cdots| z|\)
lowercase letters:\ (a|b|c|\cdots|z|\)

Every atom in our virtual chemistry has its own atomic mass, giving you the mass of the atom, and your program must output a molecular mass expressed in a molecule. The mass of the molecule is defined as the sum of the mass of all the included atoms. For example, suppose \ (h\) and \ (he\) have atomic mass of \ (1\) and \ (4\), then \ ((h_2he) _2\) The molecular weight is \ (12\) .

Input output Format input format:

The input is made up of two parts. The first part is the atomic table, which consists of a number of rows, each containing an atomic symbol, one or more spaces, and the atomic mass of the atom (\ (\leq 1000\)). No two lines contain the same atomic symbol.

The first part finally includes only a single line of string "END_OF_FIRST_PART" .

The second part is the sequence of some rows. Each line is a formula, not more than \ (80\) characters, and does not include spaces. A molecule consists of a maximum of \ (10^5\) atoms, and atoms in some molecules may not appear in the Atomic table.

The last line is only one 0, which indicates the end of the input.

Output format:

The output is a sequence of some rows, the same number as the second branch of the input file. If each atom in the molecule appears in an atomic table, it outputs an integer and represents the molecular mass. No output unknown. Do not output extra characters.

Input and Output Sample input example:
H 1He 4C 12O 16F 19Ne 20Cu 64Cc 333END_OF_FIRST_PARTH2C(MgF)2AsCu(OH)2H((CO)2F)990
Sample output:
14UNKNOWN987426
Exercises

The first time to write the stack to simulate, re+wa, simply deleted the re-use of Dfs wrote again on the (\cdots \cdots\)

Pure simulation, not much to say.

#include <iostream> #include <map> #include <string> #include <cstdio> #include <cctype> Std::map<std::string, int> elements;std::string matter;int cur, weight, length;inline int GetInt () {Register int r  ET (0);  while (cur < length && isdigit (Matter[cur])) {ret = RET * + matter[cur++]-' 0 '; } return ret? Ret:1;}  inline int Dfs () {Register int ret (0);      while (cur < length) {if (matter[cur] = = ' (') {++cur;    RET + = Dfs ();      } else if (matter[cur] = = ') ') {++cur;    return ret * GETINT (); } else {if (Isalpha (Matter[cur + 1]) && islower (matter[cur + 1]) {Register std::string cur_element        = Std::string ("") + Matter[cur] + matter[cur + 1];        if (!elements.count (cur_element)) {return-2147483647;        } ++++cur;      RET + = elements[cur_element] * GETINT ();        } else {Register std::string cur_element = std::string ("") + matter[cur]; if (!elementS.count (cur_element)) {return-2147483647;        } ++cur;      RET + = elements[cur_element] * GETINT (); }}} return ret;}    int main (int argc, char const *argv[]) {{Register std::string element;      while (std::cin >> element && Element! = "End_of_first_part") {scanf ("%d", &weight);    Elements[element] = weight;      } while (Std::cin >> matter && matter! = "0") {cur = 0, length = matter.length ();      Register int num (DFS ());      if (num < 0) {puts ("UNKNOWN");      } else {printf ("%d\n", num); }}} return 0;}

Luogu P2382 Chemical formula (analogue)

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.