习题5-9 找bug(Bug Hunt, UVa1596)
来源:互联网 发布:大数据需要掌握的技术 编辑:程序博客网 时间:2024/06/05 22:58
#include <iostream>#include <string>#include <vector>#include <stack>#include <queue>#include <deque>#include <set>#include <map>#include <algorithm>#include <sstream>#include <utility>#include <cstring>#include <cstdio>#include <cmath>#include <cctype>#define CLOSE() ios::sync_with_stdio(false)#define CLEAR(a, b) memset(a, b, sizeof(a))#define IN() freopen("in.txt", "r", stdin)#define OUT() freopen("out.txt", "w", stdout)const int maxn = 1e5 + 5;using LL = long long;using UI = unsigned int;using namespace std;//------------------------------------------------------------------------------------------//struct Array {LL capc;map<LL, pair<bool, LL>> elem;};map<string, Array> arr;int flag, row;void decl(string &s) {string name, cap;int p = s.find('[');name = s.substr(0, p);int p2 = s.find(']');cap = s.substr(p + 1, p2 - p - 1);Array a; a.capc = atoi(cap.c_str());arr[name] = a;//cout << "declaration: " << name << " cap: " << cap << endl;}pair<string, int> solvel(string &lhs) {int p = lhs.find('['), b = 0;string name;stack<string> sta;int num;while (p != -1) {name = lhs.substr(b, p - b);//cout << "name: " << name << endl;sta.push(name);b = p + 1;p = lhs.find('[', b);}p = lhs.find(']', b);if (p == -1) { num = atoi(lhs.c_str()); return { string(), num }; }else num = atoi(lhs.substr(b, p + 1).c_str());while (!sta.empty()) {name = sta.top(); sta.pop();if (num >= arr[name].capc) {flag = row;return { string(), -1 };}if (arr[name].elem[num].first == false && !sta.empty()) {flag = row;return { name, -1 };}if(!sta.empty()) num = arr[name].elem[num].second;}//cout << "{" << name << "," << num << "}\n";return { name, num };}int solver(string &lhs) {int p = lhs.find('['), b = 0;string name;stack<string> sta;int num;while (p != -1) {name = lhs.substr(b, p - b);//cout << "name: " << name << endl;sta.push(name);b = p + 1;p = lhs.find('[', b);}p = lhs.find(']', b);if (p == -1) { num = atoi(lhs.c_str()); return num; }else num = atoi(lhs.substr(b, p + 1).c_str());while (!sta.empty()) {name = sta.top(); sta.pop();if (num >= arr[name].capc) {flag = row;return -1;}if (arr[name].elem[num].first == false) {flag = row;return -1;}num = arr[name].elem[num].second;}//cout << num << "\n";return num;}void asgn(string &lhs, string &rhs) {pair<string, int> p;p = solvel(lhs); int num = solver(rhs);arr[p.first].elem[p.second].second = num;arr[p.first].elem[p.second].first = true;}void phase(string &s) {string lhs, rhs;int p = s.find('=');if (p != -1) {lhs = s.substr(0, p + 1);rhs = s.substr(p + 1);asgn(lhs, rhs);}else decl(s);}int main() {string s;IN(); OUT();while (arr.clear(), flag = 0, row = 1, getline(cin, s) && s != ".") {if (!flag) phase(s);while (row++, getline(cin, s) && s != ".") {if (!flag) phase(s);}cout << flag << endl;}return 0;}
阅读全文
0 0
- 习题5-9 找bug(Bug Hunt, UVa1596)
- 算法竞赛入门经典 第二版 习题5-9 找bug Bug Hunt uva1596
- UVa1596 - Bug Hunt
- UVA1596 Bug Hunt
- [刷题]算法竞赛入门经典(第2版) 5-9/UVa1596 - Bug Hunt
- 5-9 UVA 1596 Bug Hunt找BUG
- 紫书章五习题九 Bug Hunt UVA 1596
- UVa 1596 - Bug Hunt(细节)
- POJ 3524 Bug Hunt
- UVa 1596 - Bug Hunt
- 1596 - Bug Hunt
- UVa 1596 - Bug Hunt
- UVa:1596 - Bug Hunt
- 1596 - Bug Hunt【STL】
- UVa-1596-Bug Hunt
- UVA 1596 Bug Hunt
- 1596 - Bug Hunt
- UVA1597--Bug Hunt
- mongodb--java连接数据库实现增删改查
- 【Linux】学会 core dump 事后调试 快速定位段错误
- Maven编译报错[ERROR] Fatal Error: Unable to find package java.lang in classpath or bootclasspath
- Tempter of the Bone HDU
- nmap搭建及命令简介
- 习题5-9 找bug(Bug Hunt, UVa1596)
- mysql 存储过程 游标
- 创建、编译ros package
- WebView里面调用打电话
- lintcode 整数排序 | 希尔排序算法(Java)
- 斐波那契数列的余数周期问题
- SSH之传统方式接入分布式会话(Spring Session)篇
- springboot集成fastdfs
- Find All Numbers Disappeared in an Array(哈希表)