How can I satisfy thee? Let me count the ways... ZOJ - 3025(后缀表达式+模拟)

本文介绍了一种通过模拟中缀表达式转换为后缀表达式,并暴力枚举所有可能的赋值方法来解决特定数学问题的算法。该算法适用于需要判断表达式在不同变量赋值下是否能得到特定结果的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:QAQ

 

题意:给你一个表达式,然后给你几张表,让你枚举有多少种赋值方法能使答案为2.

 

思路:模拟一下中缀表达式转成后缀表达式,暴力枚举就行了。

 

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
#define LL long long int
char ch[100];
int nummm;
stack<char>mu;
stack<int>fin;
int jian[10] = { 2,1,0 };
int jia[5][5][5] = { { {0,0,0},{0,1,1},{0,1,2,} } ,{ { 0,1,2 },{ 1,1,2 },{ 2,2,2, } } };
int main(void) {
	while (gets(ch)) {
		if (strlen(ch) == 1 && ch[0] == '.') {
			break;
		}
		int tot = 0;
		int len = strlen(ch);
		for (int p = 0; p < 3; p++) {
			for (int q = 0; q < 3; q++) {
				for (int r = 0; r < 3; r++) {
					while (!fin.empty()) {
						fin.pop();
					}
					while (!mu.empty()) {
						mu.pop();
					}
					for (int i = 0; i < len; i++) {
						if (ch[i] == ' ') 
							continue;
						if (ch[i] == 'P' ) {
							fin.push(p);
							while(!mu.empty() && mu.top()=='-') {
								mu.pop();
								int  pp = fin.top();
								fin.pop();
								fin.push(jian[pp]);
							}
						}
						else if (ch[i] == 'Q') {
							fin.push(q);
							while (!mu.empty() && mu.top() == '-') {
								mu.pop();
								int qq = fin.top();
								fin.pop();
								fin.push(jian[qq]);
							}
						}
						else if (ch[i] == 'R') {
							fin.push(r);
							while (!mu.empty() && mu.top() == '-') {
								mu.pop();
								int rr = fin.top();
								//printf("%d\n", rr);
								fin.pop();
								fin.push(jian[rr]);
								//printf("%d\n", jian[rr]);
							}
						}
						else if (ch[i] >= '0'&&ch[i] <= '2') {
							fin.push(ch[i] - '0');
							while (!mu.empty()&&mu.top() == '-') {
								mu.pop();
								int gg = fin.top();
								fin.pop();
								fin.push(jian[gg]);
							}
						}
						else {
							if (ch[i] == '-') {
								mu.push(ch[i]);
							}
							else if (ch[i] == '(') {
								mu.push(ch[i]);
							}
							else if (ch[i] == '+') {
								if (!mu.empty() && mu.top() == '(') {
									mu.push('1');
								}
								else if (mu.size() == 0) {
									mu.push('1');
								}
								else {
									int a1 = fin.top();
									fin.pop();
									int a2 = fin.top();
									fin.pop();
									fin.push(jia[mu.top() - '0'][a1][a2]);
									mu.pop();
								}
							}
							else if (ch[i] == '*') {
								if (!mu.empty() && mu.top() == '(') {
									mu.push('0');
								}
								else if (mu.size() == 0) {
									mu.push('0');
								}
								else {
									int a1 = fin.top();
									fin.pop();
									int a2 = fin.top();
									fin.pop();
									fin.push(jia[mu.top() - '0'][a1][a2]);
									mu.pop();
								}
							}
							else if (ch[i] == ')') {
								while (mu.top()!= '(') {
									int a1 = fin.top();
									fin.pop();
									int a2 = fin.top();
									fin.pop();
									fin.push(jia[mu.top() - '0'][a1][a2]);
									//printf("%d\n", jia[mu.top() - '0'][a1][a2]);
									mu.pop();
								}
								mu.pop();
								while (!mu.empty() && mu.top() == '-') {
									mu.pop();
									int  pp = fin.top();
									fin.pop();
									fin.push(jian[pp]);
								}
							}
						}
					}
					if (mu.size() >= 1&&fin.size()==2) {
						int a1 = fin.top();
						fin.pop();
						int a2 = fin.top();
						fin.pop();
						fin.push(jia[mu.top() - '0'][a1][a2]);
						mu.pop();
						while (!mu.empty() && mu.top() == '-') {
							mu.pop();
							int  pp = fin.top();
							fin.pop();
							fin.push(jian[pp]);
						}
					}
					if (fin.size() == 1 && fin.top() == 2) tot++;
				}
			}
		}
		printf("%d\n", tot);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值