难点在于表达式计算。采用双栈方法完成。类似四则运算表达式计算题,
由于没有乘法符号,简单做法是遇到‘)’直接跳栈。能AC。但是该方法计算顺序会不完全从左到右,例如(A(BC)D)E ,计算顺序BC->BCD-ABCD
所以完成表达式计算前应该补全表达式。
            //add '*' after each letter
            //add '*' after each ')'

using System;
using System.Collections.Generic;
using System.Linq;
public class Program {
    public static void Main() {
        string line;
        while ((line = System.Console.ReadLine ()) != null) { // 注意 while 处理多个 case
            int input = Convert.ToInt32(line);
            var dic = new Dictionary<char, string>();
            for(int i = 0;i< input;i++)
            {
                dic.Add(Convert.ToChar('A'+i),Console.ReadLine());
            }
            string exp = Console.ReadLine ();
            //
            //
            //
            //1.deal with the input:
            //add '*' behind each letter
            //add '*' after each ')'
            //add '*' between ')','('
            var expTemp = exp;
            exp = "";
            for(int i = 0;i<expTemp.Length;i++)
            {
                exp = string.Concat(exp,expTemp[i]);
                if(expTemp[i] >='A' && expTemp[i] <='Z' 
                   && i != expTemp.Length -1 
                   && expTemp[i+1] != ')')
                {
                    exp = string.Concat(exp,'*');
                }//add '*' behind each letter
                else if(expTemp[i] == ')' 
                        && i != expTemp.Length -1 
                        &&(expTemp[i+1] >='A' && expTemp[i+1] <='Z' || expTemp[i+1] == '('))
                {
                    exp = string.Concat(exp,'*');
                }//add '*' after each ')'
            }
            //Console.WriteLine(exp);//-----------------------------------------debug
            //
            //
            //
            //2.compute the expression:
            var stack_matrix = new Stack<string>();
            var stack_oparetor = new Stack<char>();//contains '(',')','*'
            int result = 0;
            for(int i = 0;i<exp.Length;i++)
            {
                if(exp[i] == '(')
                {
                    stack_oparetor.Push(exp[i]);
                }
                else if(exp[i] >='A' && exp[i] <='Z')
                {
                    stack_matrix.Push(dic[exp[i]]);
                }
                else if(exp[i] == '*'&& stack_oparetor.Peek() != '*')
                {
                    stack_oparetor.Push(exp[i]);
                }
                else//when exp[i] == ')' or there is already '*' on top of stack
                {
                    if(exp[i] == '*')
                    {
                        stack_oparetor.Push(exp[i]);
                    }
                    while(stack_oparetor.Peek() == '*')
                    {
                        stack_oparetor.Pop();
                        var right = stack_matrix.Pop();
                        var left = stack_matrix.Pop();
                        stack_matrix.Push(MatrixMutiply(left, right, ref result));
                    }
                    stack_oparetor.Pop();//pop '('
                }
            }
            foreach(var item in dic)
            {
                var a = item.Key;
                var b = item.Value;
                //Console.WriteLine(a+" :"+b);//----------------------------------------------------------debug
            }
            Console.WriteLine(result);
        }
    }
    public static string MatrixMutiply(string left,string right,ref int sum)
    {
        var leftList = left.Split(' ').Select(p=>Convert.ToInt32(p)).ToList();
        var rightList = right.Split(' ').Select(p=>Convert.ToInt32(p)).ToList();
        sum += leftList[0]*leftList[1]*rightList[1];
        //Console.WriteLine(leftList[0]+"*"+leftList[1]+"*"+rightList[1]+"=>"+sum);//----------------------------debug
        return leftList[0] + " " + rightList[1];
    }
}