[刷题]算法竞赛入门经典(第2版) 5-9/UVa1596 - Bug Hunt
//开学了,好烦啊啊啊啊啊!怎么开个学那么多破事情!!都俩星期了,终于有时间写出来一道题
题意:不难理解,不写了。这几天忙的心累。
代码:(Accepted, 0.010s)
//UVa1596 - Bug Hunt
#include<iostream>
#include<sstream>
#include<string>
#include<stack>
#include<map>
using namespace std;
struct o_O {
int size;
map<int, int> dim;
};
map<string, o_O> dat;
string line;
bool get(stack<string>& st,int& num) {
while (!st.empty()) {
if (num >= dat[st.top()].size) return false;
if (!dat[st.top()].dim.count(num)) return false;
num = dat[st.top()].dim[num];
st.pop();
}
return true;
}
bool declare() {//定义数组
for (auto& r : line)
if (r == '[' || r == ']') r = ' ';
istringstream in(line);
string na;
int nu;
in >> na >> nu;
dat[na].size = nu;
return nu >= 0;
}
bool solve(){//处理赋值
for (auto& r : line)
if (r == '[' || r == ']' || r == '=') r = ' ';
stack<string> stl, str;
int numl, numr;
istringstream in(line);
string now,sl;
in >> sl;
while (in >> now && now[0] > '9') stl.push(now);
istringstream inl(now);inl >> numl;
while (in >> now && now[0] > '9') str.push(now);
istringstream inr(now);inr >> numr;
if (!get(stl, numl) || !get(str, numr)) return false;
if (numl >= dat[sl].size) return false;
dat[sl].dim[numl] = numr;
return true;
}
int main()
{
//freopen("in.txt", "r", stdin);//
while (cin>>line && line[0] != '.') {
unsigned flag = 0, linenum = 0;
dat.clear();
do {
++linenum;
if (flag) continue;
if (!(line.find('=') == string::npos ? declare() : solve()))
flag = linenum;
} while (cin>>line && line[0] != '.');
cout << flag << '\n';
}
return 0;
}
分析:给数组找bug,只有赋值和定义两种语句。一开始我想复杂了,以为会有诸如
a[b[c[0]=1]=d[2]]=e[e[e[e[e[e[3]]]=e[1]=e[2]=e[3]=233]]]=1
这种情况发生,于是就用递归做,做了好久,还老是WA(/* 实在难抽出一块一块的时间来学习,断断续续写了一星期,思路老是想到一半就断了。而且事情多烦的脑子疼,想不出东西。妈蛋还不如放假。*/)
结果昨天晚上一个老司机跟我说,干嘛那么烦,一行只有一个赋值。。。。。
好的,瞬间简单了。。今天花了一个小时不知道到不到就做出来了。可能是用了sstream,再转存到stack的原因,比较慢,用时10ms。
附:之前以为一行可以多个赋值的时候写的代码(更新:终于调试的AC了):
代码:(Accepted, 0.010s)
//UVa1596 - Bug Hunt
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<map>
using namespace std;
string line;
queue<string> qu;
struct o_O {
int len;
map<int, int> def;
};
map<string, o_O> dat;
int get() {//从qu中get当前index的值,通过递归实现
string now = qu.front();
qu.pop();
if (now[0] > '9') {//若为变量名,说明有嵌套
int n = get();//得到当前变量的index
qu.pop();
if (n >= dat[now].len || n < 0) return -1;//数组越界
if (qu.empty() || qu.front()[0] == '}') {//不是赋值,直接返回
if (!dat[now].def.count(n)) return -1;//没初始化
return dat[now].def[n];
}
return dat[now].def[n] = get();//赋值
}
int num;//若为数字
istringstream iin(now);
iin >> num;
return num;
}
bool declare() {//定义数组
for (auto& r : line)
if (r == '[' || r == ']') r = ' ';
istringstream in(line);
string na;
int nu;
in >> na >> nu;
dat[na].len = nu;
return nu >= 0;
}
bool solve() {//预处理当前行
for (auto& r : line)
if (r == '[' || r == '=') r = ' ';
istringstream in(line);
string now;
int num;
while (in >> now) {
if (now[0] > '9') qu.push(now);
else {
int i = 0;
for (auto& r : now)
if (r == ']') r = ' ', ++i;
qu.push(now);
while (i--) qu.push("}");//为什么要用}不用],一开始想的是}的ASCII是125,而]夹在大小写字母之间。然而后来发现并没有什么卵用
}
}
return get() >=0;
}
int main()
{
//freopen("in.txt", "r", stdin);//
while (getline(cin, line) && line[0] != '.') {
unsigned flag = 0, linenum = 0;
dat.clear();
while (!qu.empty()) qu.pop();//queue不自带clear()的?!
do {
++linenum;
if (flag) continue;
if (!(line.find('=') == string::npos ? declare() : solve()))
flag = linenum;
} while (getline(cin, line) && line[0] != '.');
cout << flag << '\n';
}
return 0;
}
虽然是我想多了,但是应用到单个赋值也是没问题的呀。但是还是想不通哪里就WA了。。。应该是哪个特殊的格式没考虑到。以后有心情再调试。(更新:终于调试得AC啦,代码已经覆盖更新。竟然也是0.010s。)
[刷题]算法竞赛入门经典(第2版) 5-9/UVa1596 - Bug Hunt的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...
- [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci
题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...
- [刷题]算法竞赛入门经典(第2版) 5-13/UVa822 - Queue and A
题意:模拟客服MM,一共有N种话题,每个客服MM支持处理其中的i个(i < N),处理的话题还有优先级.为了简化流程方便出题,设每个话题都是每隔m分钟来咨询一次.现知道每个话题前来咨询的时间.间 ...
- [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...
- [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation
题意:模拟Petri网的执行.虽然没听说过Petri网,但是题目描述的很清晰. 代码:(Accepted,0.210s) //UVa804 - Petri Net Simulation //Accep ...
- [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile
题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...
- [刷题]算法竞赛入门经典(第2版) 6-1/UVa673 6-2/UVa712 6-3/UVa536
这三题比较简单,只放代码了. 题目:6-1 UVa673 - Parentheses Balance //UVa673 - Parentheses Balance //Accepted 0.000s ...
- [刷题]算法竞赛入门经典(第2版) 5-16/UVa212 - Use of Hospital Facilities
题意:模拟患者做手术. 其条件为:医院有Nop个手术室.准备手术室要Mop分钟,另有Nre个恢复用的床.准备每张床要Mre分钟,早上Ts点整医院开张,从手术室手术完毕转移到回复床要Mtr分钟.现在医院 ...
- [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary
题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...
- [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web
题意:不难理解,照搬题意的解法. 代码:(Accepted,0.190s) //UVa1597 - Searching the Web //#define _XIENAOBAN_ #include&l ...
随机推荐
- iwebshop插件的操作
<?php class Miao extends pluginBase { //插件名字 public static function name(){ return "秒杀" ...
- python服务器环境搭建(3)——参数配置
前面我们已安装好了python服务器运行所需要的相关软件,而最重要最繁琐的就是参数配置,写这篇就踩了好多坑,花了好多时间,遇到了各种各样的问题.好了费话少说,直接进入本篇话题. PS:本人不是专业的运 ...
- QT Creator 快速入门教程 读书笔记(三)
一 信号和槽 GUI 程序除了要绘制控件,还要响应系统和用户事件,例如重绘.绘制完成.点击鼠标.敲击键盘等.当事件发生时,UI 会产生相应的变化,让用户直观地看到. 大部分编程(例如Win SDK ...
- JAVA-实例方法被覆盖,静态方法被隐藏Explain
被覆盖比较好理解,类似于多态的实现. 被隐藏是指静态方法的访问是根据当前对象的表面类型来决定的,比如 Supers = new Sub(); s.greeting()访问的是Super的静态方法,如果 ...
- 【转】PV3D的小练习~太阳系八大行星
转自:http://hi.baidu.com/boycy/item/70d1ba53bc8c3a958c12eddf http://www.cnblogs.com/flash3d/archive/20 ...
- 如何在IDEA中调试 Jar文件
原创文章,转载请注明出处:http://www.cnblogs.com/acm-bingzi/p/6668333.html 问题: 一般情况下,可以打成Jar包的项目,它的源码运行Applicat ...
- Python多线程Selenium跨浏览器测试
前言 在web测试中,不可避免的一个测试就是浏览器兼容性测试,在没有自动化测试前,我们总是苦逼的在一台或多台机器上安装N种浏览器,然后手工在不同的浏览器上验证主业务流程和关键功能模块功能,以检测不同浏 ...
- salesforce 零基础学习(六十九)当新增/修改一条记录以后发生了什么(适合初学者)
salesforce开发中,我们会对object进行很多的操作,比如对object设置字段的必填性唯一性等,设置validation rule实现一下相关的字段的逻辑校验,设置workflow实现某个 ...
- [Linux] PHP程序员玩转Linux系列-使用supervisor实现守护进程
1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 4.PHP程序员玩转L ...
- 在Centos中yum安装和卸载软件的使用方法
安装一个软件时 yum -y install httpd 安装多个相类似的软件时 yum -y install httpd* 安装多个非类似软件时 yum -y install httpd php p ...