题意:给定两种操作,一种是定义一个数组,另一种是赋值,让你找出哪一步时出错了,出错只有两种,一种是数组越界,另一种是访问未定义变量。

析:当初看到这个题时,感觉好麻烦啊,然后就放过去了,而现在要重新回来做一下,感觉也不好做,做了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栈)的更多相关文章

  1. UVA 1596 Bug Hunt (大模拟 栈)

    题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为 ...

  2. 【技巧性(+递归运用)】UVa 1596 - Bug Hunt

    In this problem, we consider a simple programming language that has only declarations of onedimensio ...

  3. uva 1596 Bug Hunt

    In this problem, we consider a simple programming language that has only declarations of one-dimensi ...

  4. UVa 1596 Bug Hunt (string::find && map && 模拟)

    题意 : 给出几组由数组定义与赋值构成的编程语句, 有可能有两种BUG, 第一种为数组下标越界, 第二种为使用尚未定义的数组元素, 叫你找出最早出现BUG的一行并输出, 每组以' . '号分隔, 当有 ...

  5. 【UVA】1596 Bug Hunt(模拟)

    题目 题目     分析 算是个模拟吧     代码 #include <bits/stdc++.h> using namespace std; map<int,int> a[ ...

  6. [刷题]算法竞赛入门经典(第2版) 5-9/UVa1596 - Bug Hunt

    //开学了,好烦啊啊啊啊啊!怎么开个学那么多破事情!!都俩星期了,终于有时间写出来一道题 题意:不难理解,不写了.这几天忙的心累. 代码:(Accepted, 0.010s) //UVa1596 - ...

  7. 从零开始写STL—栈和队列

    从零开始写STL-栈和队列 适配器模式 意图:将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 主要解决:主要解决在软件系统中,常常要将 ...

  8. hdu1237 简单计算器[STL 栈]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1237 题干 代码和解释 解本题时使用了STL 栈,要记得使用#include<stack>. 解本题时使用了isdigit()函 ...

  9. Bug Hunt UVA - 1596

      In this problem, we consider a simple programming language that has only declarations of onedimens ...

随机推荐

  1. libcurl 不支持https访问

    项目中使用libcurl 访问https的时候会报错,比如:“Unsupported protocol” 或者:“Protocol https not supported or disabled in ...

  2. 修改rabbitmq Web UI 监控页面的端口

    在前几天工作中遇到一个问题,部署服务器,需要用rabbitmq自带的一个web UI监控组件,但是15672的端口没有对外映射.尝试了几种办法.开始修改rabbitmq.config,rabbitmq ...

  3. JAVA WEB开发中的资源国际化

    为什么要国际化? 不同国家与地区语言,文化,生活习惯等差异.在数字,时间,语言,货币,日期,百分数等的不同. 两个名词: I18N:即资源国际化,全称为Internationalization,因为首 ...

  4. dubbo-admin 管理平台

    一.前言 dubbo的使用,其实只需要有注册中心,消费者,提供者这三个就可以使用了,但是并不能看到有哪些消费者和提供者,为了更好的调试,发现问题,解决问题,因此引入dubbo-admin.通过dubb ...

  5. javaWeb后端学习记录

    java后端学习重点: 1.java语言特性: 基础知识,集合,多线程,并发,JVM,NIO,网络编程,设计模式.  (★★★★★) jdk源码中有大量的数据结构与java语言细节.jdk源码着重看c ...

  6. models渲染字典&form表单上传文件&ajax上传文件

    {# {% for u in teacher_d.keys %}#} {# {% for u in teacher_d.values %}#} {% for k,u in teacher_d.item ...

  7. Fragment----静态创建碎片

    import android.os.Bundle; import android.app.Activity; import android.app.Fragment; import android.a ...

  8. VMware下centos7安装VMware Tools

    右键虚拟机设置,找到CD/DVD,选择使用ISO映像文件,在VMware安装目录下找到linux.iso.   挂载iso文件 > mount -t auto /dev/cdrom /mnt/c ...

  9. jquery正则判断字符串有几个逗号

    var angelweb="我,你,ta,";var re=/[,,]/g;if(re.test(angelweb)){ var n=angelweb.match(re).leng ...

  10. samba配置(z)

    http://www.cnblogs.com/mchina/archive/2012/12/18/2816717.html