【习题 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 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
随机推荐
- C#中函数的使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- channels
package main import ( "fmt" "time" "strconv") func pinger(c chan strin ...
- Markdown最简单常用的语法
1,文本强调: 文本倾斜,*我是倾斜的文本* 文本加粗,**我是加粗的文本** 文本删除线,~~带删除线的文本~~ 2,链接,分为行内式与参数式,参数式多用于在文章中多次使用相同的链接 行内式写法:[ ...
- chkconfig---检查设置系统服务
chkconfig命令 chkconfig命令检查.设置系统的各种服务.这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务 ...
- ifconfig---配置和显示Linux内核中网络接口
ifconfig命令被用于配置和显示Linux内核中网络接口的网络参数.用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在.要想将上述的配置信息永远的存的电脑里,那就要修改网卡 ...
- Matlab piecelin
function v = piecelin(x,y,u) %PIECELIN Piecewise linear interpolation. % v = piecelin(x,y,u) finds t ...
- 使WordPress改域名后网站正常运行的方法
使WordPress改域名后网站正常运行的方法 wp-content/wp-config.php $path = '/blog'; $scheme = (isset($_SERVER['HTTPS'] ...
- BZOJ3514: Codechef MARCH14 GERALD07加强版(LCT,主席树)
Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密.接下来M ...
- pycharm 配置autopep8(亲测可行)
autopep8是一个可以将Python代码自动排版为PEP8风格第三方包,使用它可以轻松地排版出格式优美整齐的代码.网络上有很多介绍如何在pycharm中配置autopep8的方案,但很多方案中还是 ...
- 常用处理字符串的SQL函数
汇总函数:Count.Sum.AVG.MAX.min; 数学函数: ABS:绝对值.floor:给出比给定值小的最大整数. round(m,n):m为给定的值,n为小数点后保留的位数. power(m ...