3.21牛客2021年度训练联盟热身训练赛第三场A.Circuit Math[栈]

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述 

You are enrolled in the Computer Organization and Architecture course at your university. You decide to write a program to help check your work by computing the output value of a combinational digital circuit, given its inputs. 

 

 

Consider the circuit shown in Figure A.1, which we use for illustration. This circuit has four inputs (letters A through D on the left), each of which is either true or false. There are four 'gates' each of which is one of three types: ANDOR, or NOT. Each gate produces either a true or false value, depending on its inputs. The last gate (the OR on the right) produces the output of the entire circuit. We can write these three types of gates in text by their equivalent logical operators: * for AND+ for OR, and - for NOT. In what follows, we'll use the operators rather than gates to describe circuits. 

 

Here is how these operators work. Given an assignment of true (T) or false (F) for each input, the operators produce the truth value indicated in the following tables: 

 


 

 

Notice that AND and OR take two inputs, whereas NOT operates on only one input. Also, we use postfix notation to write expressions involving operators (like A B *), where the operator comes after its input(s) (just as how in Figure A.1, each gate in the circuit diagram comes after its inputs). 

 

When we describe a valid circuit in postfix notation, we use the following syntax. 

  • An uppercase letter (A through Z) is a valid circuit. In other words, an input alone (without any gates) is a valid circuit (which produces as output its own input value). 
  • If <C1> and <C2> are valid circuits, then '<C1> <C2> *' is a valid circuit that produces the AND of the outputs of the two subcircuits. 
  • If <C1> and <C2> are valid circuits, then '<C1> <C2> +' is a valid circuit that produces the OR of the outputs of the two subcircuits. 
  • If <C1> is a valid circuit, then '<C1> -' is a valid circuit that produces the NOT of <C1>'s output. 

 

No other description is a valid circuit. 

 

Thus, one of the ways the circuit in Figure A.1 could be described using postfix notation is as the string: 

 

                                             A B ∗ C D + − +                                             A B ∗ C D + − +

 

Given a truth value (T or F) for each of the inputs (ABC, and D in this example), their values propagate through the gates of the circuit, and the truth value produced by the last gate is the output of the circuit. For example, when the above circuit is given inputs A = TB = FC = TD = F, the output of the circuit is F

 

Given an assignment to variables and a circuit description, your software should print the output of the circuit. 

输入描述:

 

The first line of the input consists of a single integer nn, satisfying 1≤n≤261≤n≤26, denoting the number of input variables. Then follows a line with nn space-separated characters. Each character is either TT or FF, with the i-th such character indicating the truth value of the input that is labeled with the i-th letter of the alphabet.

 

The last line of input contains a circuit description, which obeys the syntax described above. Each circuit is valid, uses only the first nn letters of the alphabet as input labels, and contains at least 11 and at most 250250 total non-space characters.

 

Note that while each variable is provided only one truth value, a variable may appear multiple times in the circuit description and serve as input to more than one gate.

 

输出描述:

Print a single character, the output of the circuit (either TT or FF), when evaluated using the given input values.

示例1

输入

复制

4
T F T F
A B * C D + - +

输出

复制

F

ac代码

#include<iostream>
#include<unordered_map> 
#include<stack>
using namespace std;
unordered_map<char, int> um;
int n;
int main() {
	cin>>n;
	char op;
	char cur='A';
	for(int i=0;i<n;i++) {
		cin>>op;
		if(op=='T')	um[cur] = 1;
		else um[cur] = 0;
		cur ++;
	}
	getchar();
	string circuit;
	getline(cin, circuit);
	int len = circuit.length();
	stack<int> stk;
	for(int i=0;i<len;i++) {
		if(circuit[i]==' ') continue;
		if(circuit[i]>='A'&&circuit[i]<='Z') stk.push(um[circuit[i]]);
		else if(circuit[i]=='*') {
			int a = stk.top();
			stk.pop();
			int b = stk.top();
			stk.pop();
			int c = a&&b;
			stk.push(c);
		}
		else if(circuit[i]=='-') {
			int a = stk.top();
			stk.pop();
			int c = !a;
			stk.push(c);
		}
		else if(circuit[i]=='+') {
			int a = stk.top();
			stk.pop();
			int b = stk.top();
			stk.pop();
			int c = a||b;
			stk.push(c);
		}
	} 
	if(stk.top()==1) cout<<'T'<<endl;
	else cout<<'F'<<endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值