1309. Assemler circuits

单点时限: 2.0 sec

内存限制: 256 MB

Bytetel Company decided to improve computers they produce. They want to replace assembler programs with special systems called assembler circuits. Assembler programs consist solely of assignments. Each assignment is determined by four elements:

1.two registers from which data are taken,

2.elementary operation that should be performed on the data,

3.register to which the result should be written.

We assume that there are at most 26 registers. They are represented by small letters of English alphabet. There are at most 4 elementary operations and they are represented by capital letters A, B, C, D.

An assembler circuit has:

1.inputs assigned to registers; initial value of appropriate register is passed to the input;

2.outputs, also assigned to registers; their final values are passed to these registers.

There are gates inside a circuit. Each gate has two inputs and one output. The gate performs an elementary operation on data delivered on its inputs and passes the result to its output. Inputs of gates and outputs of the whole circuit are connected to outputs of other gates or inputs of the circuit. Outputs of gates and inputs of the circuit can be connected to many inputs of other gates or outputs of the circuit. Connections among gates cannot form cycles.

An assembler circuit is equivalent to an assembler program if for any initial state of registers the final state of registers produced by the program and the circuit are the same.

Task

Write a program that:

1.reads a description of an assembler program;

2.computes the minimal number of gates in an assembler circuit equivalent to the given program;

3.writes the result.

输入格式

In the first line of the input file UKL.IN there is one integer n (1 <= n < 1000), which is the number of instructions in the program.

In the following n lines there are descriptions of consecutive instructions in the program. Each description is a four-letter word beginning with an elementary operation symbol: A,B,C or D. The second and the third letter (which are small letters of English alphabet) are names of registers, in which data are placed. The fourth letter is a name of a register, in which the result should be placed.

输出格式

In the first and only line of the output file UKL.OUT there should be written one integer, which is the minimal number of gates in an assembler circuit equivalent to the given program.

样例

Input
8
Afbc
Bfbd
Cddd
Bcbc
Afcc
Afbf
Cfbb
Dfdb
Output
6
A circuit equivalent to the given program is shown in the figure.

0 人解决,1 人已尝试。

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

9.9 EMB 奖励。

创建: 16 年,9 月前.

修改: 6 年,8 月前.

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

来源: POI 1998 III Stage

题目标签