【技巧性(+递归运用)】UVa 1596 - Bug Hunt
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的更多相关文章
- uva 1596 Bug Hunt
In this problem, we consider a simple programming language that has only declarations of one-dimensi ...
- UVa 1596 Bug Hunt (string::find && map && 模拟)
题意 : 给出几组由数组定义与赋值构成的编程语句, 有可能有两种BUG, 第一种为数组下标越界, 第二种为使用尚未定义的数组元素, 叫你找出最早出现BUG的一行并输出, 每组以' . '号分隔, 当有 ...
- UVA 1596 Bug Hunt (大模拟 栈)
题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为 ...
- UVa 1596 Bug Hunt (STL栈)
题意:给定两种操作,一种是定义一个数组,另一种是赋值,让你找出哪一步时出错了,出错只有两种,一种是数组越界,另一种是访问未定义变量. 析:当初看到这个题时,感觉好麻烦啊,然后就放过去了,而现在要重新回 ...
- 【UVA】1596 Bug Hunt(模拟)
题目 题目 分析 算是个模拟吧 代码 #include <bits/stdc++.h> using namespace std; map<int,int> a[ ...
- [刷题]算法竞赛入门经典(第2版) 5-9/UVa1596 - Bug Hunt
//开学了,好烦啊啊啊啊啊!怎么开个学那么多破事情!!都俩星期了,终于有时间写出来一道题 题意:不难理解,不写了.这几天忙的心累. 代码:(Accepted, 0.010s) //UVa1596 - ...
- 【习题 5-9 UVA - 1596】Bug Hunt
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] map模拟 map<string,int>记录每个数组的大小 map <pair<string, int&g ...
- Bug Hunt UVA - 1596
In this problem, we consider a simple programming language that has only declarations of onedimens ...
- C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, ...
随机推荐
- Struts2类型转换器
概述 A .从一个HTML 表单到一个 Action 对象,类型转换是从字符串到非字符串. –HTTP没有"类型" 的概念.每一项表单输入只可能是一个字符串或一个字符串数组. ...
- build.gradle 使用tips
7.查看依赖 gradlew [你想查看的module]:dependencies >dependencies.txt 6.buildToolsVersion build tools版本号 co ...
- Maven仓库Nexus的安装配置
1.下载nexus,最新版本是nexus-2.8.0-05 参考文章 下载nexus-latest-bundle.zip文件后,并解压到 D:\nexus下 配置nexus的环境变量:先配置NE ...
- jquery easyui中的formatter多用法
1.formatter能多数据进行格式化后输出,formatter必须返回一个字符串,第一个用法:当一个单元格的数据很多,不能显示完全时,鼠标放上去能动态显示出所有内容. formatter:func ...
- 48种CIFilter
48种CIFilter CIAdditionCompositing //影像合成 CIAffineTransform //仿射变换 CICheckerboardGe ...
- MVC神韵---你想在哪解脱!(十四)
修正票价字段的精度 前面我们追加数据的时候遗留下来一个问题,就是在追加数据的时候,票价(Price)字段中输入的是9.99元,但是电影清单显示画面中该数据的票价字段显示为10元,这是为什么?这个问题发 ...
- 更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found
在Xcode中当你在更新了你得证书而再重新编译你的程序,真机调试一直会出现 Code Sign error: Provisioning profile ‘XXXX’ can't be found是不是 ...
- 通过java发送http请求
通常的http请求都是由用户点击某个连接或者按钮来发起的,但是在一些后台的Java程序中需要发送一些get或这post请求,因为不涉及前台页面,该怎么办呢? 下面为大家提供一个Java发送http请求 ...
- iptables实战系列:通过NAT转发实现私网对外发布信息
原文地址: http://os.51cto.com/art/201109/289486.htm [51CTO独家特稿]本文将介绍一个使用iptables实现NAT转发功能的案例. 本文假设读者已经对N ...
- IOC运用到MVC中
IOC可以摒弃掉类中类的紧耦合,让设计和重用更简单,将IOC加入到MVC中的实现非常简单,那么有哪几种方法?它们的实现又是什么原理呢? IOC在MVC中的注入,主要是在获取Controller对象中实 ...