【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

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的更多相关文章

  1. 【技巧性(+递归运用)】UVa 1596 - Bug Hunt

    In this problem, we consider a simple programming language that has only declarations of onedimensio ...

  2. uva 1596 Bug Hunt

    In this problem, we consider a simple programming language that has only declarations of one-dimensi ...

  3. UVA 1596 Bug Hunt (大模拟 栈)

    题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为 ...

  4. UVa 1596 Bug Hunt (string::find && map && 模拟)

    题意 : 给出几组由数组定义与赋值构成的编程语句, 有可能有两种BUG, 第一种为数组下标越界, 第二种为使用尚未定义的数组元素, 叫你找出最早出现BUG的一行并输出, 每组以' . '号分隔, 当有 ...

  5. UVa 1596 Bug Hunt (STL栈)

    题意:给定两种操作,一种是定义一个数组,另一种是赋值,让你找出哪一步时出错了,出错只有两种,一种是数组越界,另一种是访问未定义变量. 析:当初看到这个题时,感觉好麻烦啊,然后就放过去了,而现在要重新回 ...

  6. 【UVA】1596 Bug Hunt(模拟)

    题目 题目     分析 算是个模拟吧     代码 #include <bits/stdc++.h> using namespace std; map<int,int> a[ ...

  7. [刷题]算法竞赛入门经典(第2版) 5-9/UVa1596 - Bug Hunt

    //开学了,好烦啊啊啊啊啊!怎么开个学那么多破事情!!都俩星期了,终于有时间写出来一道题 题意:不难理解,不写了.这几天忙的心累. 代码:(Accepted, 0.010s) //UVa1596 - ...

  8. Bug Hunt UVA - 1596

      In this problem, we consider a simple programming language that has only declarations of onedimens ...

  9. ACM训练计划建议(写给本校acmer,欢迎围观和指正)

    ACM训练计划建议 From:freecode#  Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...

随机推荐

  1. Mahout的推荐系统

    Mahout的推荐系统 什么是推荐系统 为什使用推荐系统 推荐系统中的算法 什么是推荐系统 为什么使用推荐系统? 促进厂商商品销售,帮助用户找到想要的商品 推荐系统无处不在,体现在生活的各个方面 图书 ...

  2. sql创建外键

    建立外键关系:先建主表再见从表:主表:create table zhu(code int parimary key,name varchar(20)) ;从表:create table cong(co ...

  3. Shiro + SSM(框架) + Freemarker(jsp)

    Shiro + SSM(框架) + Freemarker(jsp)讲解的权限控制Demo,还不赶快去下载? 我们知道Ajax不能做页面redirect和forward跳转,所以Ajax请求假如没登录, ...

  4. Tomcat之虚拟主机配置以及web应用配置

    Tomcat之虚拟主机配置以及web应用配置 Tomcat文件夹结构例如以下: bin ---- 启动和关闭须要的bat文件所在的文件夹 conf --- 配置文件夹 lib ---  tomcat执 ...

  5. hash_set和hash_map

    1.hash_set集合容器 hash_set利用链式哈希表,进行数据的插入.删除和搜索.与set容器同样,不同意插入反复键值的元素.SGIC++哈希表是一个链式的结构,由表头和一系列单链组成.表头是 ...

  6. web前端响应式布局,自适应全部分辨率

    写phpd的我. 近期公司要弄个app关键是没有web开发,而我有比較闲,那就扛枪上阵吧. 响应式布局,web端的?php我一直都是用tp框架,对于web首先想到的是bootstrap框架.仅仅是简单 ...

  7. hdu5400Arithmetic Sequence

    //一个序列,两个公差d1,d2 //问有多少个区间使得这个区间存在一个点,它的左边是公差为d1的序列 //它的右边是公差为d2的序列 //直接存入每一个点向左和向右延伸的公差长度,乘一下即可 //还 ...

  8. 一个令人蛋疼的NDK链接错误

    背景 我们APP的引擎包engine.so.包括了A.B.C三个project.但每次都是源代码形式编译,导致svn上存在多份同样代码拷贝. 很不科学. ..核心的Bproject由我维护.整个SO编 ...

  9. js10---call方法总结

    <html> <body> <script type="text/javascript"> function Obj(x, y){ this.x ...

  10. Android学习笔记进阶17之LinearGradient

    具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...