【习题 5-9 UVA - 1596】Bug Hunt
2024-08-31 18:38:52
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
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;
}
最新文章
- JavaScript的模块化之AMD&;CMD规范
- PHP5中使用PDO连接数据库的方法
- PHP面向对象学习六 多态
- Qt学习笔记:Qt中使用Lua
- SQL语句 - 基本查询
- JQuery 实现倒计时
- springmvc学习笔记---idea创建springmvc项目
- android中ListView_SimpleAdapter
- 转:一个跨WINDOWS LINUX平台的线程类
- Python 学习之中的一个:在Mac OS X下基于Sublime Text搭建开发平台包括numpy,scipy
- WebApi个人理解概要
- java web 之 listen 与 filter
- dicom学习文章
- Expm 4_1 多段图中的最短路径问题
- MVC常用筛选器Filter
- CABasicAnimation 划线动画
- Network - Tips
- java 数据脱敏
- git remote: error: hook declined to update
- C# 合并多个结构相同的DataTable
热门文章
- RGB-D action recognition using linear coding
- 一个一线城市的IT白领的生活成本:3万/年
- Myeclipse学习总结(3)——Myeclipse中的代码格式化、注释模板及保存时自动格式化
- RecyclerView下拉刷新和载入很多其它
- [AngularFire] Firebase OAuth Login With Custom Firestore User Data
- BZOJ 1009 GT考试 (AC自动机 + 矩阵乘法加速dp)
- golang pipe
- 【Codeforces Round #453 (Div. 2) A】 Visiting a Friend
- 一次Linux磁盘损坏导致系统不可用恢复实例
- LINUX设备驱动程序笔记(三)字符设备驱动程序