编程介的小学生 2017-08-31 08:51 采纳率: 20.5%
浏览 776
已采纳

Boring Assignment Expressions

There are multiple assignment expressions. Each of them is defined by the following BNFs.

assignment-expression ::= variable "=" expressioin
expression ::= positive-digit | variable | "(" expression "*" expression ")" | "(" expression "+" expressioin ")"
positive-digit ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
Each variable is a string of uppercase letters. The rule to calculate an assignment expression is the same as in the C programming language. An assignment cannot be calculated unless all variables on the right have assigned value from previous calculation. Variable can be assigned more than once.

Now, you need to select some assignment expressions and put them in order to make sure all variables have assigned values and the sum of the final values of all variables is minimal.

Input

Multiple test cases. For each case, the first line is the number of assignment expressions and then one expression per line.

No more than 10000 assignment expressions. No more than 2000 variables in each case. Length of variable is no more than 7. Length of each assignment expression is no more than 1000. There will always be a space beween tokens in expression. There will always be an empty line between test cases.

Output

For each test case. Output the minimal sum of final values of all variables in the first line. Then output your selected expressions one expression per line in exact form as in the input.

If there is no possible answer or the minimal sum cannot be expressed in a unsigned 64-bit integer. Output "stupid expressions!" instead.

Sample Input

2
A = ( B * 2 )
B = ( A + 1 )

7
A = 1
B = 3
C = ( ( A + B ) * A )
C = ( 2 * ( A * B ) )
C = ( ( A + ( 2 * B ) ) + A )
A = ( ( C + B ) + A )
B = ( A + 1 )
Sample Output

stupid expressions!
6
A = 1
B = ( A + 1 )
C = ( ( A + B ) * A )

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-09-15 15:45
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?