40PTS RE玄关求条,马蜂优良
查看原帖
40PTS RE玄关求条,马蜂优良
1004336
Hope5365楼主2025/2/5 19:45

RT

#include <iostream>
#include <string>
#include <map>
#include <stack>
using namespace std;

const int N = 1e3 + 10;
string yuju[N];
int n,end_place[N],rev_cnt,rev_tmp[N];
map <char,int> zhi;
map <char,int> comp;

inline void find_end_place(int step)
{
	int pos = step;
	while (1)
	{
		if (yuju[pos] == "end") break;
		else if (yuju[pos].size() > 3 && yuju[pos].substr(0,4) == "loop")
		{
			find_end_place(pos + 1);
			pos = end_place[pos + 1] + 1;
		}
		else pos ++;
	}
	end_place[step] = pos;
}

inline void init()
{
	while (getline(cin,yuju[++n]) && yuju[n] != "") ;
	n --;
	for(int i = 2;i <= n;i ++) yuju[i - 1] = yuju[i];
	n --;
	for(int i = 1;i <= n;i ++)
	{
		int pos = 0;
		while (pos < yuju[i].size() && yuju[i][pos] == ' ') pos ++;
		yuju[i] = yuju[i].substr(pos);
	}
	for(int i = 1;i <= n;i ++) end_place[i] = -1;
	find_end_place(1);
}

inline int to_int(string s)
{
	int sum = 0;
	for(int i = 0;i < s.size();i ++) sum = sum * 10 + s[i] - '0';
	return sum;
}

inline int oper(int x,int y,char c)
{
	if (c == '+') return x + y;
	if (c == '-') return x - y;
	if (c == '*') return x * y;
	return x / y;
}

inline string turn_houzhui(string s)
{
	stack <char> st;
	while (!st.empty()) st.pop();
	string ans = "";
	for(int i = 0;i < s.size();i ++)
	{
		if (s[i] >= '0' && s[i] <= '9')
		{
			int pos = i;
			ans += "&";
			while (1)
			{
				ans += s[pos];
				pos ++;
				if (pos == s.size()) break;
				if (s[pos] > '9' || s[pos] < '0') break;
			}
			i = pos - 1;
		}
		else if (s[i] >= 'a' && s[i] <= 'z')
		{
			ans += "&";
			rev_cnt = 0;
			int total = zhi[s[i]];
			while (total)
				rev_tmp[++rev_cnt] = total % 10,total /= 10;
			for(int j = rev_cnt;j >= 1;j --)
				ans += char(rev_tmp[j] + '0');
		}
		else if (s[i] == '(') st.push('(');
		else if (s[i] == ')')
		{
			while (1)
			{
				if (st.top() == '(') break;
				ans += st.top(),st.pop();
			}
			st.pop();
		}
		else
		{
			while (!st.empty() && comp[st.top()] >= comp[s[i]]) ans += st.top(),st.pop();
			st.push(s[i]);
		}
	}
	while (!st.empty()) ans += st.top(),st.pop();
	return ans;
}

inline int eval(string s)
{
	stack <int> st;
	while (!st.empty()) st.pop();
	for(int i = 0;i < s.size();i ++)
	{
		if (s[i] == '&')
		{
			int sum = 0;
			while (s[i + 1] >= '0' && s[i + 1] <= '9') i ++,sum = sum * 10 + s[i] - '0';
			st.push(sum);
		}
		else
		{
			int y = st.top();
			st.pop();
			int x = st.top();
			st.pop();
			st.push(oper(x,y,s[i]));
		}
	}
	return st.top();
}

inline int sum_string(string s)
{
	s = turn_houzhui(s);
	return eval(s);
}

inline void dfs(int step,int cnt)
{
	bool flag = 0;
	for(int j = 1;j <= cnt;j ++)
	{
		for(int i = step;i <= end_place[step];i ++)
		{
			string s = yuju[i];
			if (s == "continue") break;
			if (s == "break")
			{
				flag = 1;
				break;
			}
			if (s.substr(0,4) == "loop")
			{
				int loop_cnt = sum_string(s.substr(5));
				dfs(i + 1,loop_cnt);
				i = end_place[i + 1];
			}
			else if (s.substr(0,5) == "write")
			{
				int answer = sum_string(s.substr(6));
				cout << answer << endl;
			}
			else
			{
				char name = s[0];
				int answer = sum_string(s.substr(2));
				zhi[name] = answer;
			}
		}
		if (flag) break;
	}
}

int main()
{
	ios::sync_with_stdio(0);
	init();
	comp['+'] = comp['-'] = 1;
	comp['*'] = comp['/'] = 2;
	dfs(1,1);
	return 0;
}
2025/2/5 19:45
加载中...