跳到正文

个人记录

U579104 题解(题解没法发)

danny_wuzhiheng约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年5月4日
更新
2026年5月4日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

题目U579104

思路:

如下表,级别

+-*/()没有#
+?>
-?>
*>><<?>
/>><<?>
(?>
)=?>
#?=

∴\therefore可以得到

>> 当前符号需要入栈

<< 栈顶符号需要出栈运算

== 直接出栈

AC代码

C++
#include<bits/stdc++.h>
using namespace std;
int s_data[10005],s_op[10005],dtop,otop;
int n,m,pro[7][7]=
{
//行表示当前符号,列栈顶符合,
//0表示小于,即当前符号优先级小于栈顶符号优先级需要出栈运算
//1表示大于当前符号需要入栈,-1表示相等
//  + - * / ( ) #
	{0,0,0,0,1,0,1},
	{0,0,0,0,1,0,1},
	{1,1,0,0,1,0,1},
	{1,1,0,0,1,0,1},
	{1,1,1,1,1,0,1},
	{0,0,0,0,-1,0,1},
	{0,0,0,0,0,0,-1}
};
string s;
int ch=0;
int ci(char c){
	if(c=='+')return 0;
	if(c=='-')return 1;
	if(c=='*')return 2;
	if(c=='/')return 3;
	if(c=='(')return 4;
	if(c==')')return 5;
	if(c=='#')return 6;

}
int ok =1;
int main(){
	cin >>s;
	s='#'+s+'#';
	dtop=otop=-1;
	otop++;
	s_op[otop]=6;
	for(int i=1;i<s.size();i++)
    {
		if(s[i]>='0'&&s[i]<='9')
        {
			ch=ch*10+(s[i]-'0');
            ok=1;
		}
        else
        {
            if (ok == 1)
            {
                dtop++;
    			s_data[dtop]=ch;
                ch=0;
                ok=0;
            }
			int cur=ci(s[i]);
			int l=s_op[otop];

			while(pro[cur][l]==0)
            {
				int ans;
				if(l==0)ans=s_data[dtop-1]+s_data[dtop];
				if(l==1)ans=s_data[dtop-1]-s_data[dtop];
				if(l==2)ans=s_data[dtop-1]*s_data[dtop];
				if(l==3)ans=s_data[dtop-1]/s_data[dtop];
				otop--;
				dtop--;
				s_data[dtop]=ans;
				l=s_op[otop];
			}
			if(pro[cur][l]==1)
            {
				otop++;
				s_op[otop]=cur;
			}
			else{
				otop--;
			}
		}
	}
	cout<<s_data[dtop];
    return 0;
}
原文