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. IMAQ Flatten Image to String VI的参数设置对比

    无压缩 jpeg压缩 无损二元包装 仅JPEG压缩时有效 平化类型(指定字符串中存储什么类型的数据)   None JPEG PACKED BINARY Quality Image Image and ...

  2. Chapter 8. Classes

    8.1. Class Declarations 8.1.1. Class Modifiers 8.1.1.1. abstract Classes 8.1.1.2. final Classes 8.1. ...

  3. cookie.js 加载顺序问题

    今天遇到一个问题,在使用cookie.js时,只有在jquery.js文件后加载整体才有效 有效加载顺序 <head> <script type="text/javascr ...

  4. phper 要求

    做了这么多年php,今天看到一个07年的老文,才发现自己的水平太菜.转过来激励下自己 说句实话,写这个真够无聊的.本来看了某位大虾的类似文章,腹诽了几句也就算了.但是昨天晚上有个客户拿着这篇文章问我: ...

  5. PI-webservice06-调用外部webservice过程中注意问题

    1,SAP与.NET系统之间通过webservice来进行数据交互的过程中,格式是有要求的,要求.NET发布出来的webservice中的数据是用list来进行传输的,不能用datatable和lis ...

  6. Codeforces Beta Round #51 C. Pie or die 博弈论找规律 有趣的题~

    C. Pie or die Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem/ ...

  7. Sublime Text 备忘

    Sublime Text已经被传成编程利器,那当然也是我们前端的利器了,刚开始用的时候,很多小问题,所以做个备忘,忘记的时候也可以翻出来看看,下次重装的时候可以用到. 1.设置自动换行 菜单栏 Vie ...

  8. 数据挖掘十大经典算法(9) 朴素贝叶斯分类器 Naive Bayes

    贝叶斯分类器 贝叶斯分类器的分类原理是通过某对象的先验概率,利用贝叶斯公式计算出其后验概率,即该对象属于某一类的概率,选择具有最大后验概率的类作为该对象所属的类.眼下研究较多的贝叶斯分类器主要有四种, ...

  9. 1039. Course List for Student (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1039 题目: 1039. Course List for Student (25) 时间限制 2 ...

  10. 【v2.x OGE教程 19】 引擎状态控制

    1.手机button监听 OGE中提供了在BaseGameLauncher(GameLauncher的父类)和IScene(Scene实现的接口)中定义了onKeyUp和onKeyDown的方法.使得 ...