Problem:
Problem E: Molecular Formula Your mission in this problem is to write a computer program that manipulates molecular for-
mulae in virtual chemistry . As in real chemistry, each molecular formula represents a molecule
consisting of one or more atoms. However, it may not have chemical reality. The following are the definitions of atomic symbols and molecular formulae you should consider.  An atom in a molecule is represented by an atomic symbol, which is either a single capital
  letter or a capital letter followed by a small letter. For instance H and He are atomic
  symbols.  A molecular formula is a non-empty sequence of atomic symbols. For instance, HHHeHHHe
  is a molecular formula, and represents a molecule consisting of four H’s and two He’s.  For convenience, a repetition of the same sub-formula where n is an integer
  between 2 and 99 inclusive, can be abbreviated to ( X ) n . Parentheses can be omitted if
  X is an atomic symbol. For instance, HHHeHHHe is also written as H2HeH2He, (HHHe)2,
  (H2He)2, or even ((H)2He)2.  The set of all molecular formulae can be viewed as a formal language. Summarizing the above
description, the syntax of molecular formulae is defined as follows.   Each atom in our virtual chemistry has its own atomic weight. Given the weights of atoms,
your program should calculate the weight of a molecule represented by a molecular formula.
The molecular weight is defined by the sum of the weights of the constituent atoms. For
instance, assuming that the atomic weights of the atoms whose symbols are H and He are 1 and
4, respectively, the total weight of a molecule represented by (H2He)2 is 12. Input The input consists of two parts. The first part, the Atomic Table, is composed of a number of
lines, each line including an atomic symbol, one or more spaces, and its atomic weight which is a positive integer no more than 1000. No two lines include the same atomic symbol. The first part ends with a line containing only the string END OF FIRST PART. The second part of the input is a sequence of lines. Each line is a molecular formula, not
exceeding 80 characters, and contains no spaces. A molecule contains at most 10 5 atoms. Some
atomic symbols in a molecular formula may not appear in the Atomic Table. The sequence is followed by a line containing a single zero, indicating the end of the input. Output The output is a sequence of lines, one for each line of the second part of the input. Each line
contains either an integer, the molecular weight for a given molecular formula in the correspond-
ing input line if all its atomic symbols appear in the Atomic Table, or UNKNOWN otherwise. No
extra characters are allowed. Sample Input H 1
He 4
C 12
O 16
F 19
Ne 20
Cu 64
Cc 333
END_OF_FIRST_PART
H2C
(MgF)2As
Cu(OH)2
H((CO)2F)99
0 Output for the Sample Input 14
UNKNOWN
98
7426

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def _kosa(a1, a2, b1, b2):
    x1,y1,_ = a1
    x2,y2,_ = a2
    x3,y3,_ = b1
    x4,y4,_ = b2

    tc = (x1-x2)*(y3-y1)+(y1-y2)*(x1-x3)
    td = (x1-x2)*(y4-y1)+(y1-y2)*(x1-x4)
    return tc*td < 0

def kosa(a1, a2, b1, b2):
    return _kosa(a1,a2,b1,b2) and _kosa(b1,b2,a1,a2)


def distance(x1, y1, x2, y2):
    return math.sqrt((x1-x2)**2 + (y1-y2)**2)

def distance3(p1, p2, p3):
    x1,y1,_ = p1
    x2,y2,_ = p2
    x3,y3,_ = p3

    ax = x2 - x1
    ay = y2 - y1
    bx = x3 - x1
    by = y3 - y1

    r = (ax*bx + ay*by) / (ax*ax+ay*ay)
    if r <= 0:
        return distance(x1,y1, x3,y3)
    if r >= 1:
        return distance(x2,y2, x3,y3)
    return distance(x1 + r*ax, y1 + r*ay, x3,y3)

def main():
    rr = []

    d = {}

    while True:
        s = S()
        if s == 'END_OF_FIRST_PART':
            break
        a,b = s.split()
        d[a] = int(b)

    def f(s):
        a = []
        for c in s:
            if "0" <= c <= "9":
                if len(a) > 0 and isinstance(a[-1], int):
                    a[-1] *= 10
                    a[-1] += int(c)
                else:
                    a.append(int(c))
            elif c in string.ascii_lowercase:
                if len(a) < 1 or isinstance(a[-1], int):
                    return 'UNKNOWN'
                a[-1] += c
            else:
                a.append(c)
        for c in a:
            if isinstance(c, int) or c == '(' or c == ')':
                continue
            if c not in d:
                return 'UNKNOWN'

        def _f(a):
            if '(' in a:
                i = a.index('(')
                c = 1
                for j in range(i+1,len(a)):
                    if a[j] == '(':
                        c += 1
                    elif a[j] == ')':
                        c -= 1
                        if c == 0:
                            t = _f(a[i+1:j])
                            if len(a) > j+1 and isinstance(a[j+1], int):
                                t *= a[j+1]
                                return _f(a[:i] + [-t] + a[j+2:])
                            return _f(a[:i] + [-t] + a[j+1:])
                return None
            r = 0
            for i in range(len(a)):
                c = a[i]
                if isinstance(c, int):
                    r += abs(c)
                    continue
                if len(a) > i+1 and isinstance(a[i+1], int) and a[i+1] >= 0:
                    r += (d[c]-1) * a[i+1]
                else:
                    r += d[c]
            return r
        return _f(a)


    while True:
        s = S()
        if s == '0':
            break
        rr.append(f(s))

    return '\n'.join(map(str, rr))



print(main())

Solution (Python):
dic = {}

while 1:
    line = input()
    if line == "END_OF_FIRST_PART":
        break
    index,num = line.split()
    dic[index] = int(num)

def solve(st):
    
    if st == "0":
        return False
    ans = 0    
    kigou = ""
    mass = 0
    num = ""
    i = 0
    while i < len(st):
        
       
        if st[i] in ['0','1','2','3','4','5','6','7','8','9']:
            if len(kigou)>0:
                mass = dic[kigou]
            if mass == "UNKNOWN":
                return mass
            kigou = ""
            if  i+1<len(st) and st[i+1]  in ['0','1','2','3','4','5','6','7','8','9']:
                ans += mass * int(st[i:i+2])
                i+=1
     
            else:
                ans += mass * int(st[i])
      
        elif st[i] == '(':
            if len(kigou)>0:
                if not(kigou in dic.keys()):
                    return "UNKNOWN"
                ans += dic[kigou]
                kigou = ""
            j = i+1
            c = 1
            while c>0:
                if st[j] =='(':
                    c +=1
                if st[j] == ')':
                    c -=1
                j += 1
            mass = solve(st[i+1:j-1])
            i = j-1
            kigou=""

        elif str.isupper(st[i]):
            if len(kigou) > 0:
                if not(kigou in dic.keys()):
                    return "UNKNOWN"
                ans += dic[kigou]
            kigou = st[i]
        else:
            kigou += st[i]
        i+=1
        
    if len(kigou)>0:
        if not(kigou in dic.keys()):
            return "UNKNOWN"
        ans += dic[kigou]
    return ans

while True:
    ans = solve(input())
    if ans:
        print(ans)
    else:
        break

Solution (Python):
weights = {}


def calc(expr):
    ans = 0
    i = 0
    l = len(expr)
    while i < l:
        c = expr[i]
        if c == "(":
            expr2 = ""
            depth = 1
            i += 1
            while i < l and depth > 0:
                c2 = expr[i]
                if c2 == "(":
                    depth += 1
                elif c2 == ")":
                    depth -= 1
                if depth == 0:
                    break
                expr2 += c2
                i += 1
            if depth > 0:
                raise SyntaxError("invalid syntax")
            w = calc(expr2)
        elif "A" <= c <= "Z":
            name = c
            if i + 1 < l and "a" <= expr[i + 1] <= "z":
                name += expr[i + 1]
                i += 1
            w = weights[name]
        else:
            raise SyntaxError("invalid syntax")
        if i + 1 < l and "1" <= expr[i + 1] <= "9":
            num = expr[i + 1]
            i += 1
            if i + 1 < l and "0" <= expr[i + 1] <= "9":
                num += expr[i + 1]
                i += 1
            k = int(num)
        else:
            k = 1
        ans += w * k
        i += 1
    return ans



while True:
    line = raw_input()
    if line == "END_OF_FIRST_PART":
        break
    symbol, weight = line.split()
    weights[symbol] = int(weight)


while True:
    line = raw_input()
    if line == "0":
        break
    try:
        print calc(line)
    except KeyError:
        print "UNKNOWN"

Solution (Python):


from collections import deque
import itertools as it
import sys

sys.setrecursionlimit(1000000)

m = {}
m2 = {}

def check(S):
    return ord('0') <= ord(S) and ord(S) <= ord('9')

while True:
    S = raw_input()
    if S == 'END_OF_FIRST_PART':
        break
    atom, w = S.split()
    if len(atom) >= 2:
        m2[atom] = w
    else:
        m[atom] = w

while True:
    S = raw_input()
    if S == '0':
        break
    SS = ''
    for i in range(len(S) - 1):
        SS += S[i]
        if not check(S[i]) and check(S[i + 1]):
            SS += '*'
        if check(S[i]) and not check(S[i + 1]):
            SS += '+'
    SS += S[-1]
    S = '(' + SS + ')'
    for atom in m2:
        S = S.replace(atom, m2[atom] + '+')
    for atom in m:
        S = S.replace(atom, m[atom] + '+')
    S = S.replace('+*', '*')
    S = S.replace('+)', ')')
    try:
        ret = eval(S)
    except:
        ret = "UNKNOWN"
    print ret

Solution (Python):
from collections import defaultdict
d = defaultdict(int)

def main(s, x=1):
    f = True
    l = 0
    r = 0
    index = float("INF")
    res = 0
    i = 0
    lis = []
    if x:
        while i < len(s):
            if s[i].isupper():
                tmp = s[i]
                i += 1
                while i < len(s) and s[i].islower():
                    tmp += s[i]
                    i += 1
                lis.append(tmp)

            elif s[i].isdecimal():
                tmp = s[i]
                i += 1
                while i < len(s) and s[i].isdecimal():
                    tmp += s[i]
                    i += 1
                lis.append(tmp)

            else:
                lis.append(s[i])
                i += 1
    else:
        lis = s
    s = lis
    ans = 0
    i = 0
    
    while i < len(s):
        if s[i] == "(":
            f = False
            l += 1
            index = min(index, i)

        elif s[i] == ")":
            r += 1
            if l == r:
                res = main(s[index + 1 :i], 0)
                if res == "UNKNOWN":
                    return res
                f = True
                if i != len(s) - 1:
                    if s[i + 1].isdecimal():
                        i += 1
                        res *= int(s[i])
                ans += res
                index = float("INF")
                l = 0
                r = 0
        
        else:
            if f:
                if not (s[i] in d.keys()):
                    return "UNKNOWN"
                res = d[s[i]]
                if i != len(s) - 1:
                    if s[i + 1].isdecimal():
                        i += 1
                        res *= int(s[i])
                ans += res

        i += 1
    return ans


if __name__ == "__main__":
    while 1:
        n = input()
        if n == "END_OF_FIRST_PART":
            break
        a, b = n.split()
        b = int(b)
        d[a] = b
    while 1:
        n = input()
        if n == "0":
            break
        print(main(n))

Solution (Python):

from typing import List, Dict

atomic_table: Dict[str, int] = {}


class NoAppearAtomicSymbol(Exception):
    pass


class Token:
    s: str
    index = 0

    def __init__(self, s: str):
        self.s = s

    @property
    def c(self) -> str:
        assert self.ok
        return self.s[self.index]

    @property
    def ok(self) -> bool:
        return self.index < len(self.s)

    def next(self):
        self.index += 1


def molecule(t: Token) -> int:
    n = 0
    while t.ok:
        if t.c == '(':
            t.next()
            m = molecule(t)
            t.next()
            m *= number(t)
            n += m
        elif t.c == ')':
            break
        else:
            m = atom(t)
            m *= number(t) or 1
            n += m
    return n


def atom(t: Token) -> int:
    c = ''
    if t.c.isupper():
        c += t.c
        t.next()
    if t.ok and t.c.islower():
        c += t.c
        t.next()
    try:
        return atomic_table[c]
    except KeyError:
        raise NoAppearAtomicSymbol


def number(t: Token) -> int:
    n = 0
    while t.ok and t.c.isdigit():
        n = n * 10 + int(t.c)
        t.next()
    return n


def readuntil(sep: str) -> List[str]:
    lines = []
    while True:
        line = input()
        if line == sep:
            break
        lines.append(line)
    return lines


def main():
    global atomic_table

    atoms = [l.split() for l in readuntil('END_OF_FIRST_PART')]
    atomic_table = {k: int(v) for (k, v) in atoms}
    for seq in readuntil('0'):
        try:
            print(molecule(Token(seq)))
        except NoAppearAtomicSymbol:
            print('UNKNOWN')


main()

Solution (Python):


def mole():
    global p
    global s
    num=term()
    while len(s)>p:
        if s[p]=='+':
            p+=1
            num+=term()
        else:
            break
    return num

def term():
    global p
    global s
    num=factor()
    while len(s)>p:
        if s[p]=='*':
            p+=1
            num*=factor()
        else:
            break
    return num

def factor():
    global p
    global s
    num=0
    if len(s)>p and s[p]=='(':
        p+=1
        num=mole()
        p+=1
    else:
        return number()
    return num

def number():
    global p
    global s
    num=0
    if s[p].isdigit():
        while len(s)>p and s[p].isdigit():
            num*=10
            num+=int(s[p])
            p+=1
    elif s[p]!='(' and s[p]!=')':
        tmp=''
        while len(s)>p and s[p]!='(' and s[p]!=')' and s[p]!='+' and s[p]!='*':
            tmp+=s[p]
            p+=1
        if tmp in dic:
            num=int(dic[tmp])
        else:
            global f
            f=False
    return num

def pre():
    global s
    s=list(s)
    i=1
    while True:
        if i>=len(s):
            break
        if s[i].isdigit():
            s.insert(i,'*')
            i+=1
            while i+1<len(s) and s[i+1].isdigit():
                i+=1
        elif (s[i]=='(' or s[i].isupper()) and (s[i-1]!='('):
            s.insert(i,'+')
            i+=1
        i+=1
    s=''.join(s)

dic={}
while True:
    s=input()
    if s=='END_OF_FIRST_PART':
        break
    a,b=s.split()
    dic[a]=b
while True:
    s=input()
    if s=='0':
        break
    p=0
    f=True
    pre()
    ans=mole()
    print(ans if f else 'UNKNOWN')