2418. Final Returns of Politicians

单点时限: 3.0 sec

内存限制: 256 MB

You are a tax collector and have to check n politicians’ assets and final returns. However, a strange law has been enforced because politicians do not want to open their asset to the public. Under this law, a politician is able to open his asset to the public in an equation with other politicians: for example, the total amount of asset of politician A and politician B is C yen. Your job is to write a program to calculate the amount of each politician’s asset from given equations about politicians’ assets.

输入格式

Input consists of multiple test cases. The first line of each test case contains a single positive integer n ( n < 100 ), which indicates the number of politicians. In the following n lines, each line contains a name of politician. In the following n lines of these lines, each line contains an equation about politicians’ assets. An equation is given by a in the following BNF.

::= ‘ ‘ is ‘ ‘

::= | ‘ ‘ plus ‘ ‘

::= NAME | ‘ ‘ times ‘ ‘ NAME

::= { ‘1’ | ‘2’ | ‘3’ | ‘4’ | ‘5’ | ‘6’ | ‘7’ | ‘8’ | ‘9’ } |

::= |

::= ‘0’ | ‘1’ | ‘2’ | ‘3’ | ‘4’ | ‘5’ | ‘6’ | ‘7’ | ‘8’ | ‘9’

NAME is one of politician’s name given in the test case. You can assume that the name of a politician is contained in a sentence at most once.

Let us consider “2 times Ichiro plus 2 times Yasuo is 6344” for example. That means, the sum of double the amount of Ichiro’s asset and double the amount of Yasuo’s asset is 6,344.

Input is terminated by a case of n = 0, and it should not be processed.

You can assume that a name of politician consists of alphabetic characters, and a name of each politician differs from others. You can also assume that any integers in the input should be less than 231, and the amount of a politician’s asset should be less than 231.

输出格式

On the first line of output for each test case, you should output the test case number c that starts from 1 in the format as follows: “Case #c”. You should output a list of the amounts of politicians’ assets if it is possible to calculate all of them. You should output the list in n lines, and you should output the amount of a politician’s asset on one line in the format as follows: “NAME a”. The list should be sorted in ASCII code ascending order by NAME. You should output the amount of a politician’s asset in integers rounded off its first decimal place. Otherwise, you should output “-” instead of the list.

You should separate two test cases by a blank line.

样例

Input
2
Ichiro
Yasuo
2 times Ichiro plus 2 times Yasuo is 6344
4 times Ichiro plus 2 times Yasuo is 12544
2
Ichiro
Yasuo
1000000000 times Ichiro is 500000000
Yasuo plus Ichiro is 0
0
Output
Case #1
Ichiro 3100
Yasuo 72
Case #2
Ichiro 1
Yasuo -1

0 人解决,3 人已尝试。

0 份提交通过,共有 10 份提交。

9.9 EMB 奖励。

创建: 16 年前.

修改: 7 年,2 月前.

最后提交: 15 年,8 月前.

来源: Winter Contest 2008

题目标签