//开学了,好烦啊啊啊啊啊!怎么开个学那么多破事情!!都俩星期了,终于有时间写出来一道题

题意:不难理解,不写了。这几天忙的心累。


代码:(Accepted, 0.010s)

  1. //UVa1596 - Bug Hunt
  2. #include<iostream>
  3. #include<sstream>
  4. #include<string>
  5. #include<stack>
  6. #include<map>
  7. using namespace std;
  8. struct o_O {
  9. int size;
  10. map<int, int> dim;
  11. };
  12. map<string, o_O> dat;
  13. string line;
  14. bool get(stack<string>& st,int& num) {
  15. while (!st.empty()) {
  16. if (num >= dat[st.top()].size) return false;
  17. if (!dat[st.top()].dim.count(num)) return false;
  18. num = dat[st.top()].dim[num];
  19. st.pop();
  20. }
  21. return true;
  22. }
  23. bool declare() {//定义数组
  24. for (auto& r : line)
  25. if (r == '[' || r == ']') r = ' ';
  26. istringstream in(line);
  27. string na;
  28. int nu;
  29. in >> na >> nu;
  30. dat[na].size = nu;
  31. return nu >= 0;
  32. }
  33. bool solve(){//处理赋值
  34. for (auto& r : line)
  35. if (r == '[' || r == ']' || r == '=') r = ' ';
  36. stack<string> stl, str;
  37. int numl, numr;
  38. istringstream in(line);
  39. string now,sl;
  40. in >> sl;
  41. while (in >> now && now[0] > '9') stl.push(now);
  42. istringstream inl(now);inl >> numl;
  43. while (in >> now && now[0] > '9') str.push(now);
  44. istringstream inr(now);inr >> numr;
  45. if (!get(stl, numl) || !get(str, numr)) return false;
  46. if (numl >= dat[sl].size) return false;
  47. dat[sl].dim[numl] = numr;
  48. return true;
  49. }
  50. int main()
  51. {
  52. //freopen("in.txt", "r", stdin);//
  53. while (cin>>line && line[0] != '.') {
  54. unsigned flag = 0, linenum = 0;
  55. dat.clear();
  56. do {
  57. ++linenum;
  58. if (flag) continue;
  59. if (!(line.find('=') == string::npos ? declare() : solve()))
  60. flag = linenum;
  61. } while (cin>>line && line[0] != '.');
  62. cout << flag << '\n';
  63. }
  64. return 0;
  65. }

分析:给数组找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)

  1. //UVa1596 - Bug Hunt
  2. #include<iostream>
  3. #include<sstream>
  4. #include<string>
  5. #include<vector>
  6. #include<queue>
  7. #include<map>
  8. using namespace std;
  9. string line;
  10. queue<string> qu;
  11. struct o_O {
  12. int len;
  13. map<int, int> def;
  14. };
  15. map<string, o_O> dat;
  16. int get() {//从qu中get当前index的值,通过递归实现
  17. string now = qu.front();
  18. qu.pop();
  19. if (now[0] > '9') {//若为变量名,说明有嵌套
  20. int n = get();//得到当前变量的index
  21. qu.pop();
  22. if (n >= dat[now].len || n < 0) return -1;//数组越界
  23. if (qu.empty() || qu.front()[0] == '}') {//不是赋值,直接返回
  24. if (!dat[now].def.count(n)) return -1;//没初始化
  25. return dat[now].def[n];
  26. }
  27. return dat[now].def[n] = get();//赋值
  28. }
  29. int num;//若为数字
  30. istringstream iin(now);
  31. iin >> num;
  32. return num;
  33. }
  34. bool declare() {//定义数组
  35. for (auto& r : line)
  36. if (r == '[' || r == ']') r = ' ';
  37. istringstream in(line);
  38. string na;
  39. int nu;
  40. in >> na >> nu;
  41. dat[na].len = nu;
  42. return nu >= 0;
  43. }
  44. bool solve() {//预处理当前行
  45. for (auto& r : line)
  46. if (r == '[' || r == '=') r = ' ';
  47. istringstream in(line);
  48. string now;
  49. int num;
  50. while (in >> now) {
  51. if (now[0] > '9') qu.push(now);
  52. else {
  53. int i = 0;
  54. for (auto& r : now)
  55. if (r == ']') r = ' ', ++i;
  56. qu.push(now);
  57. while (i--) qu.push("}");//为什么要用}不用],一开始想的是}的ASCII是125,而]夹在大小写字母之间。然而后来发现并没有什么卵用
  58. }
  59. }
  60. return get() >=0;
  61. }
  62. int main()
  63. {
  64. //freopen("in.txt", "r", stdin);//
  65. while (getline(cin, line) && line[0] != '.') {
  66. unsigned flag = 0, linenum = 0;
  67. dat.clear();
  68. while (!qu.empty()) qu.pop();//queue不自带clear()的?!
  69. do {
  70. ++linenum;
  71. if (flag) continue;
  72. if (!(line.find('=') == string::npos ? declare() : solve()))
  73. flag = linenum;
  74. } while (getline(cin, line) && line[0] != '.');
  75. cout << flag << '\n';
  76. }
  77. return 0;
  78. }

虽然是我想多了,但是应用到单个赋值也是没问题的呀。但是还是想不通哪里就WA了。。。应该是哪个特殊的格式没考虑到。以后有心情再调试。(更新:终于调试得AC啦,代码已经覆盖更新。竟然也是0.010s。)

[刷题]算法竞赛入门经典(第2版) 5-9/UVa1596 - Bug Hunt的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...

  2. [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci

    题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...

  3. [刷题]算法竞赛入门经典(第2版) 5-13/UVa822 - Queue and A

    题意:模拟客服MM,一共有N种话题,每个客服MM支持处理其中的i个(i < N),处理的话题还有优先级.为了简化流程方便出题,设每个话题都是每隔m分钟来咨询一次.现知道每个话题前来咨询的时间.间 ...

  4. [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...

  5. [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation

    题意:模拟Petri网的执行.虽然没听说过Petri网,但是题目描述的很清晰. 代码:(Accepted,0.210s) //UVa804 - Petri Net Simulation //Accep ...

  6. [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile

    题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...

  7. [刷题]算法竞赛入门经典(第2版) 6-1/UVa673 6-2/UVa712 6-3/UVa536

    这三题比较简单,只放代码了. 题目:6-1 UVa673 - Parentheses Balance //UVa673 - Parentheses Balance //Accepted 0.000s ...

  8. [刷题]算法竞赛入门经典(第2版) 5-16/UVa212 - Use of Hospital Facilities

    题意:模拟患者做手术. 其条件为:医院有Nop个手术室.准备手术室要Mop分钟,另有Nre个恢复用的床.准备每张床要Mre分钟,早上Ts点整医院开张,从手术室手术完毕转移到回复床要Mtr分钟.现在医院 ...

  9. [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary

    题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...

  10. [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web

    题意:不难理解,照搬题意的解法. 代码:(Accepted,0.190s) //UVa1597 - Searching the Web //#define _XIENAOBAN_ #include&l ...

随机推荐

  1. Alamofire源码解读系列(十一)之多表单(MultipartFormData)

    本篇讲解跟上传数据相关的多表单 前言 我相信应该有不少的开发者不明白多表单是怎么一回事,然而事实上,多表单确实很简单.试想一下,如果有多个不同类型的文件(png/txt/mp3/pdf等等)需要上传给 ...

  2. POPTEST老李分享session,cookie的安全性以及区别 1

    POPTEST老李分享session,cookie的安全性以及区别   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程 ...

  3. 老李谈JVM内存模型

    老李谈JVM内存模型   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨 ...

  4. 老李推荐:第14章9节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-遍历控件树查找控件

    老李推荐:第14章9节<MonkeyRunner源码剖析> HierarchyViewer实现原理-遍历控件树查找控件   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员 ...

  5. Angular--学习

    18:28:34 Angular简介 AngularJS通过指令 扩展了HTML,并通过 表达式 绑定数据到HTML Angular扩展了HTML AngularJS 通过 ng-directives ...

  6. 在ASP.NET MVC4中配置Castle

    ---恢复内容开始--- Castle是针对.NET平台的一个非常优秀的开源项目,重点是开源的哦.它在NHibernate的基础上进一步封装,其原理基本与NHibernate相同,但它较好地解决NHi ...

  7. [原创] IAR7.10安装注册教程

    代码开发简单化的趋势势不可挡,TI 公司推出的 IAR7.10 以上版本,集成代码库,方便初学者进行学习移植.本教程详细列出IAR7.10安装以及注册步骤,不足之处望多多交流. 好了进入正题. 第一, ...

  8. 【Linux Tips】登陆,提示符,别名

    1.Linux 的tty界面下的登陆界面设置 看了半天发现,原来每次ctrl+alt+F1进入的tty1刚开始显示的就是初始化的登陆界面,顿时有种想装扮下他的冲动,因为实在是太简单了点,不过我是个喜欢 ...

  9. MongDB系列(一):使用node.js连接数据库

    1.首先启动mongodb数据库服务器 2.创建app.js,代码如下: /** * Created by byzy on 2016/8/18. * node.js 连接 mongodb实例 */ / ...

  10. 【转】JDBC学习笔记(7)——事务的隔离级别&批量处理

    转自:http://www.cnblogs.com/ysw-go/ 数据库事务的隔离级别 对于同时运行的多个事务, 当这些事务访问数据库中相同的数据时, 如果没有采取必要的隔离机制, 就会导致各种并发 ...