【习题 5-9 UVA - 1596】Bug Hunt
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
map模拟
map记录每个数组的大小
map ,int>记录数组的某个下标的值。
递归处理嵌套的情况就好
【代码】
#include <bits/stdc++.h>
using namespace std;
string s;
map <string, int> mmap;
map <pair<string, int> ,int> mmap2;
int cur = 0;
bool ok;
int gl(int l, int r)
{
return r - l + 1;
}
pair <string, string> cl(string s)
{
string val = "", temp = "";
int now = 0;
while (s[now] != '[') val += s[now++];
int len = s.size();
temp = s.substr(now + 1, gl(now + 1, len - 2));
return make_pair(val, temp);
}
int get_num(string s)
{
if (isdigit(s[0]))
{
int x = 0, lens = s.size();
for (int i = 0; i < lens; i++)
x = x * 10 + s[i] - '0';
return x;
}
string val = "";
int len = s.size(),l;
for (int i = 0; i < len; i++)
{
if (s[i] == '[')
{
l = i;
break;
}
val += s[i];
}
int nex = get_num(s.substr(l + 1, gl(l + 1, len - 2)) );
if (nex >= mmap[val] || nex < 0)
{
ok = false;
return -1;
}
else
{
if (mmap2.find(make_pair(val, nex)) != mmap2.end())
{
return mmap2[make_pair(val, nex)];
}
else
{
ok = false;
return -1;
}
}
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
while (cin >> s && s[0] != '.')
{
mmap.clear();
mmap2.clear();
ok = true;
cur = 1;
while (s[0] != '.')
{
if (!ok)
{
cin >> s;
continue;
}
int fi = s.find('=', 0);
if (fi != -1)// a=b
{
string s1 = s.substr(0, fi);
string s2 = s.substr(fi + 1);
pair <string, string> temp = cl(s1);
s1 = temp.second;
string a = temp.first;
int x = get_num(s1), y = get_num(s2);
if (!ok || x < 0 || x >= mmap[a])
{
ok = 0;
cout << cur << endl;
continue;
}
mmap2[make_pair(a, x)] = y;
}
else
{
pair <string, string> temp = cl(s);
mmap[temp.first] = get_num(temp.second);
}
cur++;
cin >> s;
}
if (ok) cout << 0 << endl;
}
return 0;
}
【习题 5-9 UVA - 1596】Bug Hunt的更多相关文章
- 【技巧性(+递归运用)】UVa 1596 - Bug Hunt
In this problem, we consider a simple programming language that has only declarations of onedimensio ...
- uva 1596 Bug Hunt
In this problem, we consider a simple programming language that has only declarations of one-dimensi ...
- UVA 1596 Bug Hunt (大模拟 栈)
题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为 ...
- UVa 1596 Bug Hunt (string::find && map && 模拟)
题意 : 给出几组由数组定义与赋值构成的编程语句, 有可能有两种BUG, 第一种为数组下标越界, 第二种为使用尚未定义的数组元素, 叫你找出最早出现BUG的一行并输出, 每组以' . '号分隔, 当有 ...
- 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 - ...
- Bug Hunt UVA - 1596
In this problem, we consider a simple programming language that has only declarations of onedimens ...
- ACM训练计划建议(写给本校acmer,欢迎围观和指正)
ACM训练计划建议 From:freecode# Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
随机推荐
- android--动态加载、插件化
需求驱动 随着业务发展需要和无线部门的拆分,各业务产品模块归属到各业务BU,原有无线App开发团队被分为基础框架.业务A.业务B.业务C等多个开发团队,从此App的开发和发布进入了一个全新模式.在这种 ...
- javaweb二
除了servlet规范,还有filter,listener.filter和servlet相似,但是在servlet之前执行,主要区别是有一个FilterChain接口可以执行拦截方法. import ...
- Redis操作使用规范
Windows 64位操作系统 Redis 安装包(当前教程版本2.8.12) 百度经验:jingyan.baidu.com 方法/步骤 1 在D盘新建文件夹[redis],右键解压Redis Z ...
- wget---从指定的URL下载文件
wget命令用来从指定的URL下载文件.wget非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性,如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕.如果是服务器打断下 ...
- CSUOJ 1554 SG Value
1554: SG Value Time Limit: 5 Sec Memory Limit: 256 MBSubmit: 140 Solved: 35 Description The SG val ...
- [Android] 图像各种处理系列文章合集
这是我近期在做Android随手拍一个项目的各种网上关于图片处理的资料,曾经学过数字图像处理都是用C++写的,以下的资料个人觉得是很优秀的各种集合,还有一方面它是在线笔记,希望对大家有所帮助吧 ...
- pat(A) 2-06. 数列求和(模拟摆竖式相加)
1.链接:http://www.patest.cn/contests/ds/2-06 2.思路:模拟摆竖式相加,因为同样位置上的数字同样,那么同一位上的加法就能够用乘法来表示 3.代码: #inclu ...
- HDU 5188 zhx and contest(带限制条件的 01背包)
Problem Description As one of the most powerful brushes in the world, zhx usually takes part in all ...
- Rsync 指令的使用方法
RsyncLinux版下载:http://rsync.samba.org/download.htmlWindows版下载:https://www.itefix.no/i2/cwrsync-get 选( ...
- Python: scikit-image gamma and log 对比度调整
这个函数,主要用来做对比度调整,利用 gamma 曲线 或者 log 函数曲线, gamma 函数的表达式: y=xγ, 其中, x 是输入的像素值,取值范围为 [0−1], y 是输出的像素值,通过 ...