UVa 1596 Bug Hunt (STL栈)
题意:给定两种操作,一种是定义一个数组,另一种是赋值,让你找出哪一步时出错了,出错只有两种,一种是数组越界,另一种是访问未定义变量。
析:当初看到这个题时,感觉好麻烦啊,然后就放过去了,而现在要重新回来做一下,感觉也不好做,做了1个多小时。。。。。
现在分析一下是思路,我觉得我想的比较麻烦,我首先定义了两个map,分别是数组的最大长度和数组的,赋值情况,然后用向量把所有操作存起来,
在定义时很简单,直接把长度赋给map就行,麻烦就是在这个赋值时,首先是把等号两边的分开,先计算等号右边的操作,主要是观察有没有出错的地方,
特别注意的是有的变量可能没定义,这样就错了,第一次没考虑WA了,两边都要考虑,最后要把右边赋值给左边,最后还要注意左边可能没有定义,
一定要特判一下。
代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <stack>
#include <string>
#include <queue>
#include <vector>
#include <cctype>
#include <sstream> using namespace std;
map<string, int> mp;
map<char, int> maxlength;
vector<string> v; int contain(const string &s){
for(int i = 0; i < s.size(); ++i)
if('=' == s[i]) return i;//等号位置
return 0;
} int cal(const string &s){
int ans = 0;
for(int i = 2; i < s.size(); ++i)//计算数组长度
if(s[i] != ']') ans = ans * 10 + s[i] - '0';
return ans;
} bool solve(string s, string ss){
stack<char> character;
int t = 0;
for(int i = 0; i < ss.size(); ++i){
if(isalpha(ss[i])) character.push(ss[i]);//把数组放进去
else if('[' == ss[i]) t = 0;
else if(isdigit(ss[i])) t = t * 10 + ss[i] - '0';//计算下标
else if(']' == ss[i]){
char ch = character.top(); character.pop();
if(!maxlength.count(ch)) return false;//这个数组不存在
if(maxlength[ch] <= t) return false;//数组越界
stringstream ss; ss << t;//把int型转string
string tt; ss >> tt;
string s1; s1 = ch + tt;
if(!mp.count(s1)) return false;//变量没定义
t = mp[s1];
}
} int ans = t; t = 0;
while(!character.empty()) character.pop();
for(int i = 0; i < s.size(); ++i){
if(isalpha(s[i])) character.push(s[i]);
else if('[' == s[i]) t = 0;
else if(isdigit(s[i])) t = t * 10 + s[i] - '0';
else if(']' == s[i]){
char ch = character.top(); character.pop();
stringstream ss; ss << t;
string tt; ss >> tt;
string s1; s1 = ch + tt;
if(!maxlength.count(ch)) return false;
if(maxlength[ch] <= t) return false;
if(i == s.size()-1){ if(!maxlength.count(s1[0])) return false; mp[s1] = ans; break; }
if(!mp.count(s1)) return false;
t = mp[s1];
}
}
return true;
} int main(){
// freopen("in.txt", "r", stdin);
string s;
while(cin >> s && s != "."){
mp.clear();
maxlength.clear();
v.clear();
v.push_back(s);
while(cin >> s && s != ".") v.push_back(s);
bool ok = false;
for(int i = 0; i < v.size(); ++i){
s = v[i];
int t = contain(s);//计算等号的位置
if(t){
string s1 = s.substr(0, t);//等号左边
string s2 = s.substr(t+1, string::npos);//等号右边
if(!solve(s1, s2)){ ok = true; printf("%d\n", i+1); break; }//出错了
}
else maxlength[s[0]] = cal(s);//定义数组
}
if(!ok){ printf("0\n"); }
}
return 0;
}
UVa 1596 Bug Hunt (STL栈)的更多相关文章
- UVA 1596 Bug Hunt (大模拟 栈)
题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为 ...
- 【技巧性(+递归运用)】UVa 1596 - Bug Hunt
In this problem, we consider a simple programming language that has only declarations of onedimensio ...
- uva 1596 Bug Hunt
In this problem, we consider a simple programming language that has only declarations of one-dimensi ...
- UVa 1596 Bug Hunt (string::find && map && 模拟)
题意 : 给出几组由数组定义与赋值构成的编程语句, 有可能有两种BUG, 第一种为数组下标越界, 第二种为使用尚未定义的数组元素, 叫你找出最早出现BUG的一行并输出, 每组以' . '号分隔, 当有 ...
- 【UVA】1596 Bug Hunt(模拟)
题目 题目 分析 算是个模拟吧 代码 #include <bits/stdc++.h> using namespace std; map<int,int> a[ ...
- [刷题]算法竞赛入门经典(第2版) 5-9/UVa1596 - Bug Hunt
//开学了,好烦啊啊啊啊啊!怎么开个学那么多破事情!!都俩星期了,终于有时间写出来一道题 题意:不难理解,不写了.这几天忙的心累. 代码:(Accepted, 0.010s) //UVa1596 - ...
- 从零开始写STL—栈和队列
从零开始写STL-栈和队列 适配器模式 意图:将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 主要解决:主要解决在软件系统中,常常要将 ...
- hdu1237 简单计算器[STL 栈]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu1237 题干 代码和解释 解本题时使用了STL 栈,要记得使用#include<stack>. 解本题时使用了isdigit()函 ...
- Bug Hunt UVA - 1596
In this problem, we consider a simple programming language that has only declarations of onedimens ...
随机推荐
- Ubuntu下启动 Redis时, 提示 "Can't open the log file: Permission denied failed"
问题来源:在删除var目录下的log文件时,将redis文件夹删除了.然后在重启时:/etc/init.d/redis-server start,提示: Starting redis-server: ...
- Java Magic. Part 1: java.net.URL
Java Magic. Part 1: java.net.URL @(Base)[JDK, url, magic, 黑魔法] 英文原文 转载请写明:原文地址 系列文章: -Java Magic. Pa ...
- cocos2d-x 3.0 学习笔记: 一个可以拖拽的Label及schedule的应用
#ifndef _DRAGLABEL_H_ #define _DRAGLABEL_H_ #include "cocos2d.h" USING_NS_CC; class DragLa ...
- bash: ifconfig: command not found 问题解决
ifconfig使用出现问题了?竟然提示找不到~~于是百度~~ [flymouse@localhost /]$ ifconfig 提示:“bash: ifconfig: command not fou ...
- java 包的命名规范
- Java对称与非对称加密解密,AES与RSA
加密技术可以分为对称与非对称两种. 对称加密,解密,即加密与解密用的是同一把秘钥,常用的对称加密技术有DES,AES等 而非对称技术,加密与解密用的是不同的秘钥,常用的非对称加密技术有RSA等 为什么 ...
- sql server profiler 的使用
sql server profiler 是作为监听sql语句执行的软件, 主要是看NTUserName,system是系统的,看自己数据库的名字.
- centos 系统下查看时间时区以及修改
1.系统时间查看及修改: Centos 6 查看系统时间:# date
- spine
spine 英 [spʌɪn] 美 [spaɪn] n.脊柱;[动,植] 棘,刺(如刺猬和海胆的刺);鱼鳍的刺;植物上的刺
- unity3d-ngui UIScrollView 滚动方向与滚轮相反
生成一个滚动面板之后发现滚轮向上滚,界面向下:滚轮向下界面向上.在编辑窗口里发现这个选项 本来是-2,修改成正数就可以了. http://ju.outofmemory.cn/entry/146754