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. JavaScript,通过分析Array.prototype.push重新认识Array

    在阅读ECMAScript的文档的时候,有注意到它说,数组的push方法其实不仅限于在数组中使用,专门留作通用方法.难道是说,在一些类数组的地方也可以使用?而哪些是和数组非常相像的呢,大家或许一下子就 ...

  2. JEECMS-自定义标签[list]

    1.自定义标签类 import static cn.com.yhxl.common.web.freemarker.DirectiveUtils.OUT_LIST; import static free ...

  3. 33条C#、.Net经典面试题目及答案[zt]

    33条C#..Net经典面试题目及答案[zt] 本文集中了多条常见的C#..Net经典面试题目例如“.NET中类和结构的区别”.“ASP.NET页面之间传递值的几种方式?”,并简明扼要的给出了答案,希 ...

  4. 【MyLocations】标记位置App开发体会

    实现功能: 1.能通过Cora Location获取地址信息 2.用户获取地址信息后能编辑相关信息 3.使用Core Data保存数据 4.使用MapKit,在Map上显示标记的位置,并可以编辑位置信 ...

  5. Android 4.2原生支持从右到左的文字排列格式

    Android 4.1(Jelly Bean)  在TextView和EditText 元素里对“双向文字顺序”提供了有限的功能支持,允许应用程序在编辑和显示字符的时候,能够同时支持从左到右(LTR) ...

  6. Windows命令大全

    From:http://technet.microsoft.com/zh-cn/library/cc731728(v=ws.10).aspx Adprep Append Arp Assoc At At ...

  7. VS2015创建的Asp.net WebApi默认项目在CentOS7+Mono4.2.2+jexus5.8运行不起来的解决方案

    主要原因是Web.config配置的问题. 修改成如下内容: <?xml version="1.0" encoding="utf-8"?> < ...

  8. MySQL安装详解(V5.5 For Windows)

    前言 这几年一直在用MySQL,并且是Windows+.Net+MySQL的搭配,用MyISAM引擎支持过单表每天千万以上的数据递增,TB级的数据MySQL游刃有余.最近在做一个较大并发的项目,尝试了 ...

  9. JQuery Basic Features Quick Walkthrough

    1. Basic Selectors $('p')—Accesses all the paragraph elements in the HTML file $('div')—Accesses all ...

  10. CSS模块化

    1. Base2. Layout3. Module4. State5. Theme 1) Base rules Base rules are the defaults. eg: ;; } input[ ...