Stack Machine Executor

题目链接:

http://acm.hust.edu.cn/vjudge/problem/26636

Description


http://7xjob4.com1.z0.glb.clouddn.com/7df4b3c52d86b5aca54ec47060e43f56

Input


The input contains description of several machines. Each machine is described by two parts: the
program and the input section.
The program is given by a series of instructions, one per line. Every instruction is given by three
uppercase letters and there must not be any other characters. The only exception is the NUM instruction,
which has exactly one space after the three letters followed by a non-negative integer number
between 0 and 109
. The only allowed instructions are those defined above. Each program is terminated
by a line containing the word ‘END’ (and nothing else).
The input section starts with an integer N (0 ≤ N ≤ 10000), the number of program executions.
The next N lines contain one number each, specifying an input value Vi
, 0 ≤ Vi ≤ 109
. The program
should be executed once for each of these values independently, every execution starting with the stack
containing one number — the input value Vi
.
There is one empty line at the and of each machine description. The last machine is followed by
a line containing the word ‘QUIT’. No program will contain more than 100 000 instructions and no
program requires more than 1 000 numbers on the stack in any moment during its execution.

Output


For each input value, print one line containing the output value for the corresponding execution, i.e.,
the one number that will be on the stack after the program executes with the initial stack containing
only the input number.
If there is a program failure during the execution or if the stack size is incorrect at the end of the
run (either empty or there are more numbers than one), print the word ‘ERROR’ instead.
Print one empty line after each machine, including the last one.

Sample Input


DUP
MUL
NUM 2
ADD
END
3
1
10
50
NUM 1
NUM 1
ADD
END
2
42
43
NUM 600000000
ADD
END
3
0
600000000
1
QUIT

Sample Output


3
102
2502
ERROR
ERROR
600000000
ERROR
600000001

Source


2016-HUST-线下组队赛-1


##题意:

给出一系列与栈相关的指令序列.
求对每一组指令集给定一个初始值时,是否会出现非法操作,如果不会则输出栈中唯一的数.


##题解:

首先明确会ERROR的情况:
操作数不够、除/模零、结果超出限制、结果栈的元素个数不是一.
一开始以为数据规模太大直接模拟可能过不了,而实际上数据非常小...
对各种操作进行模拟即可,注意各种细节的处理. (除和模与内置运算一致,不需要重载) (注意字符串的读入和数据大小)
以下的代码中,先对指令序列出现操作数不够的情况进行了预处理,方便后面的判断.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define maxn 401000
#define MAX 1000000000
#define inf 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt", "r", stdin);
#define OUT freopen("out.txt", "w", stdout);
using namespace std;

define NUM 0

define POP 1

define INV 2

define DUP 3

define SWP 4

define ADD 5

define SUB 6

define MUL 7

define DIV 8

define MOD 9

int cnt;

char op[50];

int ops[maxn];

LL op_num[maxn];

bool flag;

stack s;

int transop() {

if (op[0] == 'N') return NUM;

else if (op[0] == 'P') return POP;

else if (op[0] == 'I') return INV;

else if (op[0] == 'D' && op[1] == 'U') return DUP;

else if (op[0] == 'A') return ADD;

else if (op[0] == 'S' && op[1] == 'W') return SWP;

else if (op[0] == 'S' && op[1] == 'U') return SUB;

else if (op[0] == 'M' && op[1] == 'U') return MUL;

else if (op[0] == 'D' && op[1] == 'I') return DIV;

else if (op[0] == 'M' && op[1] == 'O') return MOD;

}

int main()

{

//IN;

while(scanf("%s", op) != EOF)
{
if (op[0] == 'Q') break; cnt = 0; flag = 1;
while(!s.empty()) s.pop();
int stack_num = 1; while (1) {
if (op[0] == 'E') break;
ops[++cnt] = transop();
if(ops[cnt] == NUM) {
scanf("%lld", &op_num[cnt]);
stack_num++;
}
if(ops[cnt] == POP) {
if(stack_num < 1) flag = 0;
stack_num--;
}
if(ops[cnt] == INV) {
if(stack_num < 1) flag = 0;
}
if(ops[cnt] == DUP) {
if(stack_num < 1) flag = 0;
stack_num++;
}
if(ops[cnt] == SWP) {
if(stack_num < 2) flag = 0;
}
if(ops[cnt] == ADD || ops[cnt] == SUB || ops[cnt] == MUL || ops[cnt] == DIV || ops[cnt] == MOD) {
if(stack_num < 2) flag = 0;
stack_num--;
} scanf("%s", op);
} if(stack_num != 1) flag = 0; int m; scanf("%d", &m);
while(m--) {
LL x;
scanf("%lld", &x);
while(!s.empty()) s.pop();
s.push(x);
if(!flag) {
puts("ERROR"); continue;
} bool flag2 = 1;
for(int i=1; i<=cnt; i++) {
if(ops[i] == NUM) {
s.push(op_num[i]);
}
if(ops[i] == POP) {
s.pop();
}
if(ops[i] == INV) {
LL cur = s.top(); s.pop();
s.push(-cur);
}
if(ops[i] == DUP) {
s.push(s.top());
}
if(ops[i] == SWP) {
LL cur1 = s.top(); s.pop();
LL cur2 = s.top(); s.pop();
s.push(cur1);
s.push(cur2);
}
if(ops[i] == ADD) {
LL cur1 = s.top(); s.pop();
LL cur2 = s.top(); s.pop();
if(cur1 + cur2 > MAX || cur1 + cur2 < -MAX) {
flag2 = 0;
break;
}
s.push(cur1 + cur2);
}
if(ops[i] == SUB) {
LL cur1 = s.top(); s.pop();
LL cur2 = s.top(); s.pop();
if(cur2 - cur1 > MAX || cur2 - cur1 < -MAX) {
flag2 = 0;
break;
}
s.push(cur2 - cur1);
}
if(ops[i] == MUL) {
LL cur1 = s.top(); s.pop();
LL cur2 = s.top(); s.pop();
if(cur2 * cur1 > MAX || cur2 * cur1 < -MAX) {
flag2 = 0;
break;
}
s.push(cur2 * cur1);
}
if(ops[i] == DIV) {
LL cur1 = s.top(); s.pop();
LL cur2 = s.top(); s.pop();
if(!cur1) {
flag2 = 0;
break;
}
LL tmp = cur2 / cur1;
s.push(tmp);
}
if(ops[i] == MOD) {
LL cur1 = s.top(); s.pop();
LL cur2 = s.top(); s.pop();
if(!cur1) {
flag2 = 0;
break;
}
LL tmp = cur2 % cur1;
s.push(tmp);
}
} if(!flag2) puts("ERROR");
else printf("%lld\n", s.top());
} printf("\n");
} return 0;

}

UVALive 5888 Stack Machine Executor (栈+模拟)的更多相关文章

  1. UVALive 5880 Vigenère Cipher Encryption (模拟)

    Stack Machine Executor 题目链接: http://acm.hust.edu.cn/vjudge/problem/26628 Description http://7xjob4.c ...

  2. UVALive 3486/zoj 2615 Cells(栈模拟dfs)

    这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...

  3. UVALive 7454 Parentheses (栈+模拟)

    Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...

  4. 【栈模拟dfs】Cells UVALive - 3486

    题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...

  5. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  6. 【LintCode·容易】用栈模拟汉诺塔问题

    用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...

  7. 51Nod 1289 大鱼吃小鱼 栈模拟 思路

    1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...

  8. Code POJ - 1780(栈模拟dfs)

    题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...

  9. HDOJ 4699 Editor 栈 模拟

    用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

随机推荐

  1. Windows 下搭建LDAP服务器

    五一闲来没事,加上项目正在进行UAT.抽空研究了一下LDAP相关知识.随手做一个记录. 为了方便阅读还是先介绍一下什么是LDAP? 前言.Lightweight Directory Access Pr ...

  2. XML文件读取工具类

    /// <summary> /// Author: jiangxiaoqiang /// </summary> public class XmlReader { //===== ...

  3. chrome浏览器下禁制 textarea改变大小; Jquery的textareaCounter插件控制textarea输入的字符数量

    给  textarea 添加一个css 样式即可 resize: none;   用Jquery的插件控制textarea输入的字符数量 一:引用Jquery脚本,并引入 textareaCounte ...

  4. 计算机网络——TCP与UDP协议详解

    根据应用程序的不同需求,运输层需要两种不同的运输协议,即面向连接的TCP和无连接的UDP. TCP:传输控制协议 TCP特点: 1)TCP是面向连接的运输层协议.所以,应用程序在使用TCP协议之前,必 ...

  5. 扩容盘、SD卡扩容

    内存卡的前世今生回想当年,大家都还在用着非智能机,由于功能单一,需要存储的数据也就是通讯录和短信.虽然那时也有手机游戏,但大多都是几十KB,并不需要太大的存储空间.但随着手机功能的多样化,尤其是音乐. ...

  6. 安卓 Dialogs(对话框)

    转载自:http://www.apkbus.com/home.php?mod=space&uid=679028&do=blog&id=61197 对话框是一个小的窗口用以提示用 ...

  7. Android的IPC机制(一)——AIDL的使用

    综述 IPC(interprocess communication)是指进程间通信,也就是在两个进程间进行数据交互.不同的操作系统都有他们自己的一套IPC机制.例如在Linux操作系统中可以通过管道. ...

  8. Xcode各版本官方下载及百度云盘下载, Mac和IOS及Xcode版本历史.

    官方下载, 用开发者账户登录,建议用Safari浏览器下载. 官方下载地址: https://developer.apple.com/xcode/downloads/ 百度云盘下载地址: http:/ ...

  9. jQuery autoComplete 样式

    前提:使用了jQuery-ui 官网:http://jqueryui.com/autocomplete/ /*** autocomplete ***/ .ui-widget-content { bac ...

  10. poj 2409(polya定理模板)

    题意:给你n种颜色和m个小球,问你有多少种不同的方案! 分析:作为模板.. 代码实现: #include <iostream> #include <cstdio> #inclu ...