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. Delphi使用FindClass实现动态建立对像(有点像反射)

    相关资料:http://www.blogjava.net/nokiaguy/archive/2008/05/10/199739.html { http://www.blogjava.net/nokia ...

  2. iOS学习之触摸事件

    触摸事件 iOS中的事件: 在用户使用app过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: view的触摸事件处理: 响应者对象: 在iOS中不是任何对象都能处理事件,只有继承了UIR ...

  3. [iOS微博项目 - 1.3] - 内容对齐 TextAlignment & VerticalAlignment & HorizontalAlignment & contentMode

    四个容易混淆的属性:1. textAligment : 文字的水平方向的对齐方式1> 取值NSTextAlignmentLeft      = 0,    // 左对齐NSTextAlignme ...

  4. [iOS基础控件 - 6.9.1] 聊天界面Demo 代码

    框架:   所有代码文件:   Model: // // Message.h // QQChatDemo // // Created by hellovoidworld on 14/12/8. // ...

  5. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  6. java 2 8 10 16

    An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or ...

  7. HQL和Criteria

    HQL: public boolean doCreate(Dept vo) throws Exception { return this.sessionFactory.getCurrentSessio ...

  8. hdu 5745 La Vie en rose DP + bitset优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...

  9. jsp页面显示数据库的数据信息表

    在日常jsp开发中:最基本的一个操作之一是把之前添加到数据库中的信息在jsp页面中显示出来,也就是增删改查中的查找的一部分: 下面是以上部分的开发步骤及分析. 1.在jsp页面: <thead& ...

  10. ADO.NET 快速入门(三):从存储过程获取输出参数

    一些存储过程通过参数返回值.当参数在SQL表达式或者存储过程中被定义为“输出”,参数值会返回给调用者.返回值存储在 OleDbCommand 或者 SqlCommand 对象的参数集合的参数里.   ...