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

析:当初看到这个题时,感觉好麻烦啊,然后就放过去了,而现在要重新回来做一下,感觉也不好做,做了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. ABAP-长文本处理

  2. Struts2 利用AJAX 导出大数据设置遮罩层

    Struts2 利用AJAX 导出大数据设置遮罩层 需求背景: 每次我们导出excel的时候 ,如果数据量很大,导出花费的时间会很长,页面却有没人任何反应,这个时候用户会认为系统有问题,要么关了页面, ...

  3. 自动选择最佳特征进行分类-SVM (Halcon)

    HALCON12里的example,classify_pills_auto_select_features.hdev. 执行流程: 1.选取相关特征(本例选取color和region组的所有特征)(本 ...

  4. C#整合VS2010和NUnit

    软件下载 .Net单元测试工具 NUnit下载:http://www.nunit.org/index.php?p=download,最新的为NUnit-2.6.0.12051.msi,下载安装. VS ...

  5. win10磁盘碎片整理

    如果我们想要加快win10系统运行速度的话,就需要定期整理碎片才可以,减少卡顿,提高性能. 一:注意事项 固态硬盘用户千万不要使用‘磁盘碎片整理功能’,因为使用的技术不一样,使用window自带的该功 ...

  6. express.Router

    [express.Router] 1.可使用 express.Router 类创建模块化.可挂载的路由句柄.Router 实例是一个完整的中间件和路由系统,因此常称其为一个 “mini-app”. 下 ...

  7. php71 gdnz

    更新yum库:yum updat yum install epel-release yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel ...

  8. 第六章 图(b1)邻接矩阵

  9. TensorFlow—CNN—CIFAR数据集分类

  10. Hook钩子编程

    钩子(Hook),是Windows消息处理机制的一个平台,钩子实际上是一个处理消息的程序段,通过系统调用,把它挂入系统,以监视指定窗口的某种消息.每当特定的消息发出,在没有到达目的窗口前,钩子程序就先 ...