跳到正文

个人记录

`

yh2022mayu约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月26日
篇幅
约 4 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define x first
#define y second
#define vc vector
#define vci vc<int>
#define psb push_back
using namespace std;
int n, m, x[10], cnt, s;
int op[500005], a[500005], b[500005];
int dfs(int id){
	int ed;
	for(int i = 1; i <= a[id]; i++)
		for(int nw = id + 1;;){
			if(cnt >= s) return 0;
//			cout << nw << ' ' << id << ' ' << a[nw] << endl;
			if(op[nw] == 1) nw = dfs(nw) + 1;
			else if(op[nw] == 2) x[a[nw]] ^= x[b[nw]], cnt++, nw++;
			else if(op[nw] == 3) x[a[nw]] &= x[b[nw]], cnt++, nw++;
			else if(op[nw] == 4) x[a[nw]] |= x[b[nw]], cnt++, nw++;
			else if(op[nw] == 5) x[a[nw]] ^= b[nw], cnt++, nw++;
			else if(op[nw] == 6) x[a[nw]] &= b[nw], cnt++, nw++;
			else if(op[nw] == 7) x[a[nw]] |= b[nw], cnt++, nw++;
			else{
				ed = nw;
				break;
			}
		}
	return ed;
}
int main(){
	cin >> n >> m;
	op[0] = 1, a[0] = 1;
	for(int i = 1; i <= n; i++){
		string s;
		cin >> s;
		if(s == "repeat") op[i] = 1, cin >> a[i];
		if(s == "xor") op[i] = 2, cin >> s, a[i] = (s[0] - 'a'), cin >> s, b[i] = (s[0] - 'a');
		if(s == "and") op[i] = 3, cin >> s, a[i] = (s[0] - 'a'), cin >> s, b[i] = (s[0] - 'a');
		if(s == "or") op[i] = 4, cin >> s, a[i] = (s[0] - 'a'), cin >> s, b[i] = (s[0] - 'a');
		if(s == "xori") op[i] = 5, cin >> s, a[i] = (s[0] - 'a'), cin >> b[i];
		if(s == "andi") op[i] = 6, cin >> s, a[i] = (s[0] - 'a'), cin >> b[i];
		if(s == "ori") op[i] = 7, cin >> s, a[i] = (s[0] - 'a'), cin >> b[i];
		if(s == "end") op[i] = 8;
//		cout << s << endl;
	}
	op[n + 1] = 4;
	while(m--){
		cnt = 0;
		cin >> s;
		for(int i = 0; i < 4; i++) cin >> x[i];
//		for(int i = 0; i < 4; i++) cout << x[i] << ' ';
//		cout << endl;
		dfs(0);
		for(int i = 0; i < 4; i++) cout << x[i] << ' ';
		cout << endl;
	}
	return 0;
}
原文