#3AC 剩下全WA求助!
查看原帖
#3AC 剩下全WA求助!
1284034
__lihaoyu68__楼主2024/8/10 09:11
#include <bits/stdc++.h>
using namespace std;
class Node {
	public:
		Node(Node* fa, int value, bool isLeaf) : father(fa), val(value), leaf(isLeaf) {return;}
		int val;
		bool leaf;
		vector <Node> child;
		Node* father;
};
int stringToInt(const string& str) {
	int num = 0;
	for (string::const_iterator i = str.begin(); i != str.end(); i++) num = (num << 3) + (num << 1) + (*i ^ 48);
	return num;
}
void U(void) {
	string str;
	int cnt = 1;
	while (cnt) {
		cin >> str;
		if (str == "loop") cnt++;
		if (str == "end") cnt--;
	}
	return;
}
Node root(NULL, 0, false);
int answer[25];
void dfs(const Node& p, int x, int y) {
	if (p.leaf) {answer[x] += y; return;}
	for (vector<Node>::const_iterator i = p.child.begin(); i != p.child.end(); i++) (i -> val == -1) ? dfs(*i, x + 1, y) : dfs(*i, x, y * i -> val);
	return;
}
int main() {
	string cmd;
	Node* ptr = &root;
	while (true) {
		cin >> cmd;
		if (cmd == "op") {
			string x;
			cin >> x;
			ptr -> child.push_back((Node) {ptr, x == "n" ? -1 : stringToInt(x), true});
		} else if (cmd == "loop") {
			string x;
			cin >> x;
			if (x == "0") x = "n";
			ptr -> child.push_back((Node) {ptr, x == "n" ? -1 : stringToInt(x), false});
			ptr = &ptr -> child.back();
		} else if (cmd == "break") {
			if (ptr == &root) continue;
			ptr -> val = 1;
			ptr = ptr -> father;
			U();
		} else if (cmd == "continue") {
			if (ptr == &root) continue;
			ptr = ptr -> father;
			U();
		} else if (cmd == "end") {
			if (ptr == &root) break;
			ptr = ptr -> father;
		}
	}
	dfs(root, 0, 1);
	bool first = true;
	for (int i = 24; ~i; i--) {
		if (answer[i]) {
			if (!first) cout << '+';
			first = false;
			if (answer[i] != 1 || !i) cout << answer[i];
			if (i > 1) cout << "n ^ " << i;
			else if (i == 1) cout << 'n';
		}
	}
	if (first) cout << 0;
	return 0;
}
2024/8/10 09:11
加载中...