UVa-1596-Bug Hunt

    解题过程中犯了几个错。其一是C++中没有itoa这个函数,想要实现数字向字符串的转换要使用to_string。其二,对于每一次赋值操作,要先检查该数组元素是否已经有值,若有,要从map中删去,再添加进新值。其三,栈中可能会有连续的数字字符,因此要将这些字符当成一个整数处理。

  
#include <iostream>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <cstdlib>
#include <algorithm>
using namespace std;
bool isDecla(string &s)
{
	for (auto &c : s)
	if (c == '=')
		return false;
	return true;
}
void createStack(stack<char> &left, stack<char> &right, string &line)
{
	auto it = find(line.begin(), line.end(), '=');
	string::iterator i;
	for (i = line.begin(); i != it; ++i)
		left.push(*i);
	for (++i; i != line.end(); ++i)
		right.push(*i);
}
pair<bool, int> processRight(map<char, map<int, int>> &dataset, map<char, int> &size, stack<char> &right)
{
	char ch;
	int n=0;
	int bit = 1;
	while (!right.empty())
	{
		ch = right.top();
		right.pop();
		if (ch == '[' || ch == ']')
			bit=1;
		else if (isdigit(ch))
		{
			n += atoi(&ch)*bit;
			bit *= 10;
		}
		else
		{
			if (n >= size[ch])
				return make_pair(false, 0);
			if (dataset.count(ch) && dataset[ch].count(n))
			{
				//right.push(dataset[ch][n] + '0');
				n = dataset[ch][n];
				string s(to_string(n));
				for (int i = 0; i<s.size(); ++i)
					right.push(s[i]);
				n = 0;
				bit = 1;
			}
			else
				return make_pair(false, 0);
		}
	}
	return make_pair(true, n);
}
bool processLeft(map<char, map<int, int>> &dataset, map<char, int> &size, stack<char> &left, int right)
{
	int n=0;
	char ch;
	int bit = 1;
	while (1)
	{
		ch = left.top();
		left.pop();
		if (isdigit(ch))
		{
			n += atoi(&ch)*bit;
			bit *= 10;
		}
		else
		{
			bit = 1;
			if (ch != '[' && ch != ']')
			{
				if (!dataset.count(ch))
					return false;
				if (n >= size[ch])
					return false;
				if (left.empty())
				{
					if (dataset[ch].count(n))
						dataset[ch].erase(n);
					dataset[ch].emplace(n, right);
					return true;
				}
				else
				{
					if (!dataset[ch].count(n))
					{
						cout << "fuck you:!dataset[ch].count(n)" << endl;
						return false;
					}
					//left.push(dataset[ch][n] + '0');
					n = dataset[ch][n];
					string s(to_string(n));
					for (int i = 0; i<s.size(); ++i)
						left.push(s[i]);
					n = 0;
				}
			}
		}
	}
}
int main()
{
	string line;
	while (getline(cin, line) && line != ".")
	{
		bool flag = true;
		int wrongLine = 0;
		char ch;
		int n;
		int cnt = 0;  //保存行数
		map<char, map<int, int>> dataset;  //保存数组信息,数组中有定义的值
		map<char, int> size;  //保存每个数组的大小
		ch = line[0];
		n = atoi(&line[2]);
		if (!isDecla(line))
		{
			wrongLine = 1;
			flag = false;
		}
		else
		{
			size.emplace(ch, n);
			dataset.emplace(ch, map<int, int>());
		}
		++cnt;
		while (getline(cin, line))
		{
			++cnt;
			if (line == ".")
			{
				if (flag)
					cout << 0 << endl;
				else
					cout << wrongLine << endl;
				break;
			}
			if (flag && isDecla(line))
			{
				ch = line[0];
				n = atoi(&line[2]);
				size.emplace(ch, n);
				dataset.emplace(ch, map<int, int>());
			}
			else if (flag) //如果不是声明
			{
				stack<char> left;
				stack<char> right;
				createStack(left, right, line);
				auto p = processRight(dataset, size, right);
				if (!p.first || !processLeft(dataset, size, left, p.second))
				{
					wrongLine = cnt;
					flag = false;
				}
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值