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:

program ::= declaration|programdeclaration|programassignment
declaration ::= array [ number ] new
assignment ::= array [ expression ]= expressionnewline
expression ::= number|array [ expression ]
number ::= digit|digitdigit
digit ::= digit|digitdigit
digit ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
digit ::= 0 | digit
array ::= a | b | c | d | e | f | g | h | i | j | k | l | m |
    n | o | p | q | r | s | t | u | v | w | x | y | z |
    A | B | C | D | E | F | G | H | I | J | K | L | M |
    N | O | P | Q | R | S | T | U | V | W | X | Y | Z

where
new
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 [ 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]
.
.

Sample Output

2
2
2
3
4
0 题意很简单,就是找bug,可能数组的下标越界或未初始化; 怎么说呢,一遇到不叫复杂的字符串题代码就越写越臭。到最后就崩溃了。这次还是找了网上的代码。更要命的是就连看人家的代码就看了一晚上。现在有些地方理解的也不透彻。技巧性比较强。自己果然需要加强这种题的锻炼。沉下心去写;
废话不说了。上代码;
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<cctype>
#include<sstream> //stringstream
using namespace std;
typedef unsigned int uint;
const int maxn = ;
int dcnt, ans;
bool bug;
vector<string> vs;
map<string, string> value;
map<string, unsigned int> mdes;
string get_val(string index, string name) //递归调用,返回下标的实数值(字符串形式)
{
uint v = ;
if(index.find('[') == index.npos) //直接是一个实数
{
stringstream ss(index); ss >> v;
if(name != " " && mdes[name] <= v) //容易理解mdes[name] <= v表示数组的下标越界
//而name == " "的情况为定义语句a[3];或赋值语句等号右值(这里理解的也不透彻。。。)
bug = true;
return index;
}
string pname, pindex;
pname = index.substr(, index.find('['));
pindex = index.substr(index.find('[')+, index.find_last_of(']')-);
pindex = get_val(pindex, pname);
if(bug) return " ";
string vv;
vv = pname+"["+pindex+"]";
if(!value.count(vv)) bug = true; //若下标未初始化,则bug
return value[vv];
}
void Define(string str)
{
string name, index; uint v = ;
name = str.substr(, str.find('['));//提取数组名
index = str.substr(str.find('[')+, str.find_last_of(']')-); //提取数组下标
index = get_val(index, " "); //此处" "意在区别赋值语句调用该函数的情况;
stringstream ss(index); ss >> v;
mdes[name] = v;//存入数组name的范围
}
void Assign(string str)
{
string L, R, name, index; uint v = ;
L = str.substr(, str.find('=')); R = str.substr(str.find('=')+);
/*等号左边*/
name = L.substr(, L.find('['));
///find_last_of();
index = L.substr(L.find('[') + , L.find_last_of(']')-);
index = get_val(index, name);
if(bug) {return;}
stringstream ss(index); ss >> v;
if(v >= mdes[name]) {bug = true; return;}
/*赋值*/
string left_value, right_value;
left_value = name+"["+index+"]";
right_value = get_val(R, " ");
value[left_value] = right_value;
}
void init()
{
mdes[" "] = ;//初始化" "代表的数组大小为0,此主要为以后求数组下标值时区别赋值语句与定义语句(及赋值语句等号右边)。
vs.clear();
mdes.clear();
value.clear();
}
int main()
{
//freopen("in.txt", "r", stdin);
string str;
while(cin >> str && str[] != '.')
{
init();
vs.push_back(str);
while(cin >> str && str[] != '.')
{
vs.push_back(str);
}
bug = false;
for(int i = ; i < vs.size(); i++)
{
if(vs[i].find('=') == vs[i].npos)
{
Define(vs[i]);
}
else
Assign(vs[i]);
if(bug)
{
cout << i+ << endl;
break;
}
if(!bug && i == vs.size()-)
cout << << endl;
}
}
return ;
}
												

【技巧性(+递归运用)】UVa 1596 - Bug Hunt的更多相关文章

  1. uva 1596 Bug Hunt

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

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

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

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

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

  4. UVa 1596 Bug Hunt (STL栈)

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

  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. 【习题 5-9 UVA - 1596】Bug Hunt

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

  8. Bug Hunt UVA - 1596

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

  9. C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件

    大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, ...

随机推荐

  1. [iOS 多线程 & 网络 - 2.6] - 使用POST上传JSON数据 & 多值参数

    A.上传JSON 1.思路: 必须使用POST方法才能上传大量JSON数据 设置请求头:设置Content-Type 设置请求体,JSON实际相当于字典,可以用NSDictionary NSJSONS ...

  2. [iOS 多线程 & 网络 - 1.2] - 多线程GCD

    A.GCD基本使用 1.GCD的概念 什么是GCD全称是Grand Central Dispatch,可译为"牛逼的中枢调度器"纯C语言,提供了非常多强大的函数GCD的优势GCD是 ...

  3. 需要熟记的git命令

    需要熟记的github常用命令 总结一下ubuntu下github常用的命令,设置部分跳过,假设repository的名字叫hello-world: .创建一个新的repository: 先在gith ...

  4. VS~单步调试DLL

    有时我们从第三方下载DLL库之后,在使用VS进行调试时还是很麻烦的,现在我总结一下,在开发过过程中调试DLL的方法,希望对各位在开发中有帮助. 1 VS下载插件.Net Refector 2 引用你的 ...

  5. ckeditor异常问题

    上传图片时点击上传按钮时,图片不能上传,有两种可能 1:采用ssh框架 , 上传图片对应的struts.xml没有配置<constant name="struts.action.exc ...

  6. zookeeper 系列

    ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分布式同步.组服务等.ZooKeeper的目标就是封装好复杂易出错的关键服务 ...

  7. maven常见问题归纳

    前言 Maven,发音是[`meivin],"专家"的意思.它是一个非常好的项目管理工具,非常早就进入了我的必备工具行列,可是这次为了把ABPM项目 全然迁移并应用maven,所以 ...

  8. some scrum screenshots

    backlog tracking progress Initial Stage Next Stage End

  9. 禁止Android的StatusBar下拉

    Android中有许多隐藏的Service,StatusBarManager就是其中一个,在Context.java中可以看到: /** * Use with {@link #getSystemSer ...

  10. Android传感器编程带实例

    看了程序人生 网站的 编程高手的编程感悟 深有感触,好像也是一个android 程序员写的,推荐大家也看看.话不多说,还是言归正传吧. 一.前言 我很喜欢电脑,可是笔记本还是太大,笔记本电脑再小还是要 ...