编程介的小学生 2020-03-12 21:00 采纳率: 20.5%
浏览 103

Logical Expression 逻辑表达式

Problem Description
Happy birthday to you!

Your professor gives you a strange device as your birthday gift. But your experience tells you, it is a trap.

Just as expected, he smirks, and then says slowly: "This device is my new invention. Only you, my most brilliant student, deserve to have it."

"Thanks, I will keep it up." You reply.

"However," silent for a while, he starts again: "unfortunately, I have lost its original circuit diagram. So I need you to do some researches on it and use a simple logical expression to describe its characteristics. You won't let me down, will you?"

He laughs, pats you on the back, then turns and stalks off, leaves you, the poor guy, with the "gift" on your hands.

You collect yourself, and start looking at the "lovely" thing. It is a black box, with n inputs labeled from the first capital letter (A) to the n-th capital letter, as well as one unique output. All the inputs and the output are one-bit binary numbers (either 0 or 1).

Then you sit down, put the device on the desk, take out the tools, examine all possible combinations of input values and record the output on the paper.

After everything gets ready, now it is just a piece of cake for you to give a correct logical expression for it.

A correct logical expression should be:
1. A not null string only contains '+', '-' and capital letters from the first (A) to the n-th;
2. A single capital letter in range is a legal logical expression, and its output equals to the value of the input labeled by this letter;
3. If E is a legal logical expression, then NOT operation -E is a legal logical expression;
4. If E and F are legal logical expressions, then AND operation EF is a legal logical expression;
5. If E and F are legal logical expressions, then OR operation E+F is a legal logical expression;
6. In a legal logical expression, you should first do NOT operations from the left to the right, then AND operations from the left to the right, and finally the OR operations from the left to the right;
7. A logical expression is correct if and only if with any possible combinations of input values, its output is the same as which the device outputs.

But as a down-to-earth idealist, you are never satisfied with this. You want to give the most beautiful logical expression like this:
1. It is a correct logical expression;
2. It is the shortest one (not null) among all correct logical expressions;
3. If there are still many available logical expressions, choose the lexicographically least one.

Input
There are several test cases in the input.

Each test case begins with one integer n (1 <= n <= 5), indicating the number of inputs of the device.
Then 2n lines follow. Each line contains (n + 1) one-bit binary numbers, separated by spaces. The first n numbers indicate the value of the n inputs in order, and the last one is the corresponding output.
The 2n sets of inputs are different with each other.
The input ends with n = 0.

Output
For each test case, output only one line -- the most beautiful logical expression.

Sample Input
3
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 0
1 1 1 1
0

Sample Output
-A-CB+-BC+AC

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘