UVa 1596 Bug Hunt (string::find && map && 模拟)
题意 : 给出几组由数组定义与赋值构成的编程语句, 有可能有两种BUG, 第一种为数组下标越界, 第二种为使用尚未定义的数组元素, 叫你找出最早出现BUG的一行并输出, 每组以' . '号分隔, 当有两组输入都是' . '时结束程序
分析 : 由于错误的类型由题意所述的两种组成, 所以我们需要知道每个数组的长度与每个已经被赋值定义过的数组元素大小, 因此可以定义map<string, int> Info 来存储数组名和这个数组的大小两个信息, 在定义一个map<string, map<int, int>> arr 来储存数组内某个元素的具体的值。由于每一行的语句可以很复杂, 类似 a[B[a[0]]]=a[B[0]] 这样, 所以需要分步处理!这里的解决方案步骤如下, 都以 c[B[a[0]]]=a[B[0]] 为例
1、如果为定义语句, 则直接进行模拟和存储即可
2、如果为赋值语句,则将其按等于号分为左右两部分进行处理 (即 c[B[a[0]]] 和 a[B[0]] )
3、提取出左部分并且分为将要被赋值的数组名 和 其[ ]内的内容 (即 c 和 B[a[0]] )
4、提取出右部分全部 (即 a[B[0]])
5、如果需要处理的数组下标, 即[ ]里的元素就是数字的话则直接判断是否存在题目所述的两种BUG, 不存在则模拟正常存储过程
6、如果[ ]元素还是嵌套的数组, 可以发现这种情况的处理方法都是从里往外一个个全部找BUG并且转成对应的数字, 符合递归思想, 因此可以写一个递归模块check()来提取
7、将第三步提取出的[ ]中的内容丢进check()进行查错和转化 (即 check( B[a[0]] )
8、将第四步提取出的内容同样丢进check()进行查错和转化
9、若所有的check()都没毛病, 则复杂的语句将会变成——>数组名[数字] = 数字 这种形式, 进行模拟存储即可
技巧 : 其中有些操作可能会比较麻烦, 下面介绍几个函数
1、字符的分部可以使用string::find_first_of( 元素 )函数, 其返回的是在string中第一次找到元素的位置 和 string::substr(begin, len)来解决
2、判断语句是否有' = '号可以使用string::find(), 如果 其!=string::npos则说明存在
瞎搞 : 在做这种模拟题前需要仔细斟酌, 否则打出来之后可能花了很多时间却发现了重大的错误, 并且需要多想用什么进行存储能方便操作, 还有尽量进行模块化编程, 否则打出来后一旦出现BUG, 对着又长又臭的代码, 改错难度肯定很大!因此三思而后行啊!
#include<bits/stdc++.h> #define in 0 #define out 0 using namespace std; map<string, int> info; map<string, map<int, int> > arr; bool Error; // check中的标志,如果为true则说明已经存在BUG int check(string s) { ])){ stringstream num(s); int k; num>>k; return k; }else{ , s.find_first_of(); , s.find_first_of('[')); int index = check(Inner); ){ Error = true; ; }else{ if(index>=info[name]){ Error = true; ; }else{ if(!arr[name].count(index)){ Error = true; ; }else{ return arr[name][index]; } } } } } bool solve() { Error = false; string temp; ;//记录行数 ;//记录错误的行数 while(cin>>temp){ ]=='.'){ if(!line) return false; else{ info.clear(); arr.clear(); printf("%d\n", error_line); return true; } } ) continue;//如果已经找到错误的行数, 没必要进行下去 line++; if(temp.find('=') != string::npos){ , temp.find_first_of('=')); ); , Left.find_first_of('[')); , Right.find_first_of('[')); , Left.find_last_of(); int L = check(InA);//丢进check进行数字的转化 int R = check(Right); if(Error){//上面进行check后如果Error为true则说明找到BUG error_line = line; }else{ if(!info.count(A)){ error_line = line; }else{ map<int, int> temp2; ){ error_line = line; }else{ if(!arr.count(A)){ temp2[L] = R; arr[A] = temp2; }else{ arr[A][L] = R; } } } } }else{ , temp.find_first_of('[')); , temp.find_first_of(); stringstream num(index); int len; num>>len; info[name] = len; } } } int main(void) { #if in freopen("in.txt", "r", stdin); #endif #if out freopen("out.txt", "w", stdout); #endif while(solve()){} ; }
UVa 1596 Bug Hunt (string::find && map && 模拟)的更多相关文章
- 【技巧性(+递归运用)】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 (大模拟 栈)
题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为 ...
- UVa 1596 Bug Hunt (STL栈)
题意:给定两种操作,一种是定义一个数组,另一种是赋值,让你找出哪一步时出错了,出错只有两种,一种是数组越界,另一种是访问未定义变量. 析:当初看到这个题时,感觉好麻烦啊,然后就放过去了,而现在要重新回 ...
- 【UVA】1596 Bug Hunt(模拟)
题目 题目 分析 算是个模拟吧 代码 #include <bits/stdc++.h> using namespace std; map<int,int> a[ ...
- 【习题 5-9 UVA - 1596】Bug Hunt
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] map模拟 map<string,int>记录每个数组的大小 map <pair<string, int&g ...
- [刷题]算法竞赛入门经典(第2版) 5-9/UVa1596 - Bug Hunt
//开学了,好烦啊啊啊啊啊!怎么开个学那么多破事情!!都俩星期了,终于有时间写出来一道题 题意:不难理解,不写了.这几天忙的心累. 代码:(Accepted, 0.010s) //UVa1596 - ...
- HDU 4287 Intelligent IME(string,map,stl,make_pair)
题目 转载来的,有些stl和string的函数蛮好的: //numx[i]=string(sx); //把char[]类型转换成string类型 // mat.insert(make_pair(num ...
- Apollo 中配置String、Map和List和默认值
摘要:在Apollo 中,配置String.Map和List等类型的信息,同时设置默认值. 综述 随着业务需求的变更,需要在Apollo中配置一个Map<String, List>类型 ...
随机推荐
- oracle truncate表 恢复操作
truncate恢复表 1.创建测试用表 conn elan/elan create table haha as select * from dba_users; 2.查询表数据 ) from hah ...
- 初步学习jquery学习笔记(五)
jquery学习笔记五 jquery遍历 什么是遍历? 从某个标签开始,按照某种规则移动,直到找到目标标签为止 标签树 <div> <ul> <li> <sp ...
- PHP 识别获取身份证号代表的信息
18位的身份证号每一位都代表什么 例如:110102197810272321 echo substr(110102197810272321,0,2)."<br>"; / ...
- Centos7 升级php版本到php7
一.首先查看是否有老版本 yum list installed | grep php 二.如果安装的有 yum remove php.x86_64 php-cli.x86_64 php-common. ...
- Ubuntu下火狐浏览器播放视频出现解码问题
问题描述 点击视频播放按钮,视频不会出现缓冲条,也没任何提示,视频界面就一直是黑屏的状态.右键该视频界面,选择检查元素,点击控制台,发现如下问题: The video on this page can ...
- PHP post调接口代码
PHP post调接口代码 /** * $url:接口地址 * $data:数组参数 **/ function postData($url, $data) { $ch = curl_init (); ...
- JSTL 的<c:if>标签没有else的解决办法
我们可以采用<c:choose>来代替<c:if> 具体结构: <c:choose> <c:when test=""> 如果 < ...
- python操作redis用法详解
python操作redis用法详解 转载地址 1.redis连接 redis提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用 ...
- Struts2.5以上版本There is no Action mapped for namespace [/] and action name [userAction_login] associated with context path []
分析:Struts2在2.5版本后添加strict-method-invocation(严格方法访问),默认为true,不能使用动态方法调用功能,故需设为false struts.xml设置如下: & ...
- Librepilot-Windows编译环境的搭建
1.安装Msys2 下载Msys2,下载地址 https://msys2.github.io,注意根据笔记本的型号选择32bit或64bit. 2.添加LibrePilot MinGW库 在 /e ...