UVa 1596 Bug Hunt (STL栈)
题意:给定两种操作,一种是定义一个数组,另一种是赋值,让你找出哪一步时出错了,出错只有两种,一种是数组越界,另一种是访问未定义变量。
析:当初看到这个题时,感觉好麻烦啊,然后就放过去了,而现在要重新回来做一下,感觉也不好做,做了1个多小时。。。。。
现在分析一下是思路,我觉得我想的比较麻烦,我首先定义了两个map,分别是数组的最大长度和数组的,赋值情况,然后用向量把所有操作存起来,
在定义时很简单,直接把长度赋给map就行,麻烦就是在这个赋值时,首先是把等号两边的分开,先计算等号右边的操作,主要是观察有没有出错的地方,
特别注意的是有的变量可能没定义,这样就错了,第一次没考虑WA了,两边都要考虑,最后要把右边赋值给左边,最后还要注意左边可能没有定义,
一定要特判一下。
代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <stack>
#include <string>
#include <queue>
#include <vector>
#include <cctype>
#include <sstream> using namespace std;
map<string, int> mp;
map<char, int> maxlength;
vector<string> v; int contain(const string &s){
for(int i = 0; i < s.size(); ++i)
if('=' == s[i]) return i;//等号位置
return 0;
} int cal(const string &s){
int ans = 0;
for(int i = 2; i < s.size(); ++i)//计算数组长度
if(s[i] != ']') ans = ans * 10 + s[i] - '0';
return ans;
} bool solve(string s, string ss){
stack<char> character;
int t = 0;
for(int i = 0; i < ss.size(); ++i){
if(isalpha(ss[i])) character.push(ss[i]);//把数组放进去
else if('[' == ss[i]) t = 0;
else if(isdigit(ss[i])) t = t * 10 + ss[i] - '0';//计算下标
else if(']' == ss[i]){
char ch = character.top(); character.pop();
if(!maxlength.count(ch)) return false;//这个数组不存在
if(maxlength[ch] <= t) return false;//数组越界
stringstream ss; ss << t;//把int型转string
string tt; ss >> tt;
string s1; s1 = ch + tt;
if(!mp.count(s1)) return false;//变量没定义
t = mp[s1];
}
} int ans = t; t = 0;
while(!character.empty()) character.pop();
for(int i = 0; i < s.size(); ++i){
if(isalpha(s[i])) character.push(s[i]);
else if('[' == s[i]) t = 0;
else if(isdigit(s[i])) t = t * 10 + s[i] - '0';
else if(']' == s[i]){
char ch = character.top(); character.pop();
stringstream ss; ss << t;
string tt; ss >> tt;
string s1; s1 = ch + tt;
if(!maxlength.count(ch)) return false;
if(maxlength[ch] <= t) return false;
if(i == s.size()-1){ if(!maxlength.count(s1[0])) return false; mp[s1] = ans; break; }
if(!mp.count(s1)) return false;
t = mp[s1];
}
}
return true;
} int main(){
// freopen("in.txt", "r", stdin);
string s;
while(cin >> s && s != "."){
mp.clear();
maxlength.clear();
v.clear();
v.push_back(s);
while(cin >> s && s != ".") v.push_back(s);
bool ok = false;
for(int i = 0; i < v.size(); ++i){
s = v[i];
int t = contain(s);//计算等号的位置
if(t){
string s1 = s.substr(0, t);//等号左边
string s2 = s.substr(t+1, string::npos);//等号右边
if(!solve(s1, s2)){ ok = true; printf("%d\n", i+1); break; }//出错了
}
else maxlength[s[0]] = cal(s);//定义数组
}
if(!ok){ printf("0\n"); }
}
return 0;
}
UVa 1596 Bug Hunt (STL栈)的更多相关文章
- UVA 1596 Bug Hunt (大模拟 栈)
题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为 ...
- 【技巧性(+递归运用)】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 (string::find && map && 模拟)
题意 : 给出几组由数组定义与赋值构成的编程语句, 有可能有两种BUG, 第一种为数组下标越界, 第二种为使用尚未定义的数组元素, 叫你找出最早出现BUG的一行并输出, 每组以' . '号分隔, 当有 ...
- 【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 - ...
- 从零开始写STL—栈和队列
从零开始写STL-栈和队列 适配器模式 意图:将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 主要解决:主要解决在软件系统中,常常要将 ...
- hdu1237 简单计算器[STL 栈]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu1237 题干 代码和解释 解本题时使用了STL 栈,要记得使用#include<stack>. 解本题时使用了isdigit()函 ...
- Bug Hunt UVA - 1596
In this problem, we consider a simple programming language that has only declarations of onedimens ...
随机推荐
- c++标准库中的string常用函数总结《转》
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- Mybatis知识(5)
1. #{} 与${}的区别 #{}解释为 JDBC prepared statement 的一个参数标记,而${}解释为字符串替换.比如:我们不能在表名的位置使用参数标记,也不能在列名的位置使用参数 ...
- MySQLReport
简介: MySQLReport 一.安装 shell > yum -y install mysqlreport perl-DBD-MySQL 二.使用 shell > mysqlrepor ...
- 批量部署ssh私钥认证
vim batch_sshkey.sh #!/bin/bashcd /rootcat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keysfor ...
- Spring的国际化(转载)
1:在MyEclipse下面创建一个test的Web Project,然后添加Spring相关的文件,在src根目录下创建applicationContext.xml文件. applicationC ...
- 调整Mic音量
uses MMSystem; function GetLineInHandle(AudioType : integer) : integer;var i : integer; AudioCaps ...
- MapReduce超时原因(Time out after 300 secs)
目前碰到过三种原因导致 Time out after 300 secs. 1. 死循环 这是最常见的原因.显式的死循环很容易定位,隐式的死循环就比较麻烦了,比如正则表达式.曾经用一个网上抄来的邮箱正则 ...
- for循环计算阶乘的和,for循环计算阶乘倒数的和
计算阶乘的和 //阶乘的和,5!+4!+3!+2! int a = 5; for(int b = 4; b > 0; b--) { a = a * b; } //先定义好最大数的阶乘是多少 in ...
- Error running : Address localhost:1099 is already in use
运行报错: Error running : Address localhost:1099 is already in use 解决方法: 打开任务管理器,将后台的java.exe进程都关掉,再次运行 ...
- gcd,扩展欧几里得,中国剩余定理
1.gcd: int gcd(int a,int b){ ?a:gcd(b,a%b); } 2.中国剩余定理: 题目:学生A依次给n个整数a[],学生B相应给n个正整数m[]且两两互素,老师提出问题: ...