In this problem, we consider a simple programming language that has only declarations of onedimensional integer arrays and assignment statements. The problem is to find a bug in the given program. The syntax of this language is given in BNF as follows:

  where ⟨new line⟩ denotes a new line character (LF).

  Characters used in a program are alphabetical letters, decimal digits, =, [, ] and new line characters. No other characters appear in a program.

  A declaration declares an array and specifies its length. Valid indices of an array of length n are integers between 0 and n − 1, inclusive. Note that the array names are case sensitive, i.e. array a and array A are different arrays. The initial value of each element in the declared array is undefined.

  For example, array a of length 10 and array b of length 5 are declared respectively as follows.

a[10] b[5]

  An expression evaluates to a non-negative integer. A ⟨number⟩ is interpreted as a decimal integer. An ⟨array name⟩ [⟨expression⟩] evaluates to the value of the ⟨expression⟩-th element of the array. An assignment assigns the value denoted by the right hand side to the array element specified by the left hand side.

Examples of assignments are as follows.

a[0]=3 a[1]=0 a[2]=a[a[1]] a[a[0]]=a[1]

  A program is executed from the first line, line by line. You can assume that an array is declared once and only once before any of its element is assigned or referred to.

  Given a program, you are requested to find the following bugs.

• An index of an array is invalid.

• An array element that has not been assigned before is referred to in an assignment as an index of array or as the value to be assigned.

  You can assume that other bugs, such as syntax errors, do not appear. You can also assume that integers represented by ⟨number⟩s are between 0 and 231 − 1 (= 2147483647), inclusive.

Input

  The input consists of multiple datasets followed by a line which contains only a single ‘.’ (period).

  Each dataset consists of a program also followed by a line which contains only a single ‘.’ (period).

  A program does not exceed 1000 lines. Any line does not exceed 80 characters excluding a new line character.

Output

  For each program in the input, you should answer the line number of the assignment in which the first bug appears. The line numbers start with 1 for each program. If the program does not have a bug, you should answer zero. The output should not contain extra characters such as spaces.

Sample Input

a[3]
a[0]=a[1]
.
x[1]
x[0]=x[0]
.
a[0]
a[0]=1
.
b[2]
b[0]=2
b[1]=b[b[0]]
b[0]=b[1]
.
g[2]
G[10]
g[0]=0
g[1]=G[0]
.
a[2147483647]
a[0]=1
B[2]
B[a[0]]=2
a[B[a[0]]]=3
a[2147483646]=a[2]
.
a[32]
b[2]
b[1]=33
b[1]=30
a[b[1]]=15
.
.

Sample Output

2
2
2
3
4
0
0

HINT

此题目的细节较多,首先补充一个测试样例:

a[32]
b[2]
b[1]=33
b[1]=30
a[b[1]]=15 0 //输出结果

这个样例的目的是检测你的程序有没有对已经赋值的元素重新正确赋值的能力,样例里面并没有,这个bug浪费了1个小时。

下面一步一步的分析:

  1. 输入的样例程序只可能右两中错误,即:数组下标越界,使用未经赋值的元素(可能同时出现在一个赋值语句中)。

  2. 所有可能出现的样例程序语句有:

    a[32]	//声明数组语句
    a[2]=1 //赋值语句
    a[a[2]]=a[2]//下标嵌套元素值
    a[a[2]]=3// 对元素的值重新赋值
  3. 如何区分声明语句以及赋值或者修改值语句?

    使用find()查找有没有‘=’,有等于号就不是声明语句。

  4. 如何存储数据?

    对数组的名以及边界采用一个map来存储,对赋值或者初始化的元素的值采用另一个map来存储,这个map的键为数组名称,值为一个map,里面存储此数组的每一个元素的值。

  5. 如何处理数据?

    对于一个正确的声明语句,第一个字母是数组的名称,最外层[]以内的部分最终一定可以计算化为一个数字。

    对于一个正确的赋值语句,等号右边一定可以化为一个数字;等号右边类似于声明语句,最外层[]以内的部分最终一定可以计算化为一个数字。

    因此,上面的讨论得到的区别就是,有没有最外层[]的问题。因此我们对[]进行单独讨论,剩下的就是可以化简称为一个数组的部分。

    对于化简成为数字的部分的操作就是栈操作,剩下的就是一些计算下标值和判断了。

Accepted

#include<bits/stdc++.h>
using namespace std;
map<int,unsigned long int>arrname; //保存数组名以及下标边界
map<int, map<unsigned long int, unsigned long int> >arrvalue; //保存已经赋值的元素
int transfor(string s, int flag,int &value) { //计算
value = 0; //初始化
stack<char>name; //保存数组名称
for (int i = 0;i < s.size();i++) { //对每一个字符判断处理
if (isalpha(s[i]))name.push(s[i]); //如果是数组名,入栈
if (s[i] >= '0' && s[i] <= '9')value = value * 10 + s[i] - '0'; //如果是数字,计算
if (s[i] == ']') { //如果是]出栈
if (!arrname.count((int)name.top())||arrname[(int)name.top()] < value || !arrvalue[(int)name.top()].count(value)) { //如果数组未声明或者下标越界或者未进行初始化的元素
if (!arrname.count((int)name.top()) && name.size() == 1 && flag == 2) {
arrname[(int)name.top()] = value; //如果他是声明语句,则声明入栈
return 1;
}
return 0; //否则,返回错误
}
value = arrvalue[(int)name.top()][value]; //如果一切正常,则计算新的下标
name.pop();
}
}
return 1;
}
int main() { string s1, s2, s;
int num = 0, flag = 1,value1,value2;
while (cin >> s) {
if (s == ".")break;
num = 0;arrname.clear();arrvalue.clear();flag = 1; //初始化
while (s != ".") {
num++;
int i = s.find('='); //判断是不是一个声明语句
if (i != - 1) { //是赋值语句
s1 = s.substr(2, i-3); //将左值最外侧最外层的[]去除
s2 = s.substr(i + 1);
if (!transfor(s1, 1, value1) || !transfor(s2, 0, value2)|| value1 >= arrname[(int)s[0]]) {flag = 0;break;} //判断是否是否合法
else arrvalue[s[0]][value1] = value2; //合法就赋值
}
else if (!transfor(s, 2,value1)) {flag = 0;break;} //声明是否合法
cin >> s;
}
if (!flag) { //结果输出
cout << num << endl;
while (s != ".")cin >> s;
}
else cout << 0 << endl;
}
}

Bug Hunt UVA - 1596的更多相关文章

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

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

  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 (大模拟 栈)

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

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

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

  6. UVa 1596 Bug Hunt (STL栈)

    题意:给定两种操作,一种是定义一个数组,另一种是赋值,让你找出哪一步时出错了,出错只有两种,一种是数组越界,另一种是访问未定义变量. 析:当初看到这个题时,感觉好麻烦啊,然后就放过去了,而现在要重新回 ...

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

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

  8. 【习题 5-9 UVA - 1596】Bug Hunt

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] map模拟 map<string,int>记录每个数组的大小 map <pair<string, int&g ...

  9. 在iOS上使用ffmpeg播放视频

    国外靠谱的有这几个:1.Mooncatventures group https://github.com/mooncatventures-group 2.KxMoviePlayer (use Open ...

随机推荐

  1. 微信小程序:小程序中使用Less

    配置: 首选项 -> 设置 -> 用户 -> 扩展 (找到EasyLess插件,编辑setting.json文件进行配置) 点击vscode左下角的à设置à点击右上角的à添加以上代码 ...

  2. MYSQL bin_log 开启及数据恢复

    参考博客: A:https://www.jianshu.com/p/55b0d52edca2 B:https://www.cnblogs.com/martinzhang/p/3454358.html ...

  3. Linux常用小命令

    1:查看当前磁盘内存 df-ah/df-hl 2:查看文件和文件夹大小 du -h --max-depth=1 /目的文件夹 3:scp 拷贝命令 指定端口传输文件 scp -p port filen ...

  4. 微信支付 V3 的 Java 实现 Payment Spring Boot-1.0.7.RELEASE 发布

    Payment Spring Boot 是微信支付V3的Java实现,仅仅依赖Spring内置的一些类库.配置简单方便,可以让开发者快速为Spring Boot应用接入微信支付. 功能特性 实现微信支 ...

  5. iOS之CoreBluetooth

    思路 手机与设备间的通讯方式CoreBluetooth是比较常见且通用的.在iOS开发中需明晰以下几点 蓝牙4.0最多可联机7个设备,iPhone6以上都是蓝牙4.0 两台iPhone并不直接通过蓝牙 ...

  6. kubernetes Pod亲和性

    三种调度粘性,主要根据官方文档说明: NodeSelector(定向调度).NodeAffinity(Node亲和性).PodAffinity(Pod亲和性). 1.      nodeSelecto ...

  7. 过多if - else 的问题, 以及策略模式 + 反射解决方法

    策略模式解决if - else 的代码 业务场景: 外包企业的审批人需要审批打卡的场景: 审批人分为多种不同的级别,多种级别中具有方式相同但是内容不同的操作:审批. 原来场景: 有前端传来审批人参数, ...

  8. LNMP配置——Nginx配置 ——访问控制

    #vi /usr/local/nginx/conf/vhost/test.com.conf 写入: server { listen 80; server_name test.com test1.com ...

  9. 来,Consul 服务发现入个门(一看就会的那种)

    前言 在微服务架构中,对于一个系统,会划分出多个微服务,而且都是独立开发.独立部署,最后聚合在一起形成一个系统提供服务.当服务数量增多时,这些小服务怎么管理?调用方又怎么能确定服务的IP和端口?服务挂 ...

  10. beego框架panic: 'GetSecurityInf' method doesn't exist in the controller CorporateInfcontroller问题解决

    在使用beego框架时,出现类似于panic: 'GetSecurityInf' method doesn't exist in the controller CorporateInfcontroll ...