习题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;}

原创粉丝点击