UVA442-栈

题意:

求矩阵相乘的次数,用栈

WA了这么多次

下次还是不能去猜题意,o(︶︿︶)o 唉

#include<iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;

struct Node
{
	int r;
	int c;
};
int main()
{
	freopen("d:\\1.txt", "r", stdin);
	int total;
	Node node[26];
	cin >> total;
	char cc;
	int r, c;
	while (total--)
	{
		cin >> cc >> r >> c;
		Node nn;
		nn.r = r;
		nn.c = c;
		node[cc-'A'] = nn;
	}
	string str;
	while (cin>>str)
	{
//		cout << str << endl;
		total = 0;
		Node stack[1000];
		int s = 0;
		bool error = false;
		int length = str.length();
		for(int i = 0; i < length; i++)
		{
			cc = str.at(i);
			if(cc == ')')
			{
				//pop;
				Node node2 = stack[--s];
				Node node1 = stack[--s];
				//r1*c1*c2
				if(node1.c != node2.r)
				{
					error = true;
					break;
				}
				total += node1.r * node1.c * node2.c;
				Node node3;
				node3.r = node1.r;
				node3.c = node2.c;
				stack[s++] = node3;
			}
			else if('A' <= cc && cc <= 'Z')
			{
				stack[s++] = node[cc - 'A'];
			}
		}
		if(error)
			cout << "error" << endl;
		else
			cout << total << endl;
	}
	return 0;
}

  

posted on 2017-05-12 23:36 好吧,就是菜菜 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6847677.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值