用栈来处理一下就好了。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std; struct Mar
{
int r,c;
} mar[];
int n;
char s[];
int r,c,top,flag;
Mar Stack[]; int main()
{
scanf("%d",&n);
for(int i=; i<n; i++)
{
scanf("%s%d%d",s,&r,&c);
mar[s[]-'A'].r=r;
mar[s[]-'A'].c=c;
}
while(~scanf("%s",s))
{
int len=strlen(s);
flag=;
top=-;
for(int i=; i<len; i++)
{
if(top==-)
{
top++;
if(s[i]>='A'&&s[i]<='Z')
Stack[top]=mar[s[i]-'A'];
else
{
Mar node;
node.c=-;
node.r=-;
Stack[top]=node;
}
continue;
}
if(s[i]=='(')
{
Mar node;
node.c=-;
node.r=-;
top++;
Stack[top]=node;
}
else if(s[i]==')')
{
top--;
Stack[top]=Stack[top+];
if(top-!=-&&Stack[top-].c!=-&&Stack[top-].r!=-)
{
if(Stack[top-].c!=Stack[top].r)
{
flag=-;
break;
}
else
{
flag=flag+Stack[top-].c*Stack[top-].r*Stack[top].c;
Stack[top-].c=Stack[top].c;
top--;
}
}
}
else
{
Mar node;
node.c=mar[s[i]-'A'].c;
node.r=mar[s[i]-'A'].r;
top++;
Stack[top]=node;
if(Stack[top-].r!=-&&Stack[top-].c!=-)
{
if(Stack[top-].c!=Stack[top].r)
{
flag=-;
break;
}
else
{
flag=flag+Stack[top-].c*Stack[top-].r*Stack[top].c;
Stack[top-].c=Stack[top].c;
top--;
}
}
}
}
if(flag==-) printf("error\n");
else printf("%d\n",flag);
}
return ;
}

POJ 2246 Matrix Chain Multiplication的更多相关文章

  1. POJ 2246 Matrix Chain Multiplication(结构体+栈+模拟+矩阵相乘)

    题意:给出矩阵相乘的表达式,让你计算需要的相乘次数,如果不能相乘,则输出error. 思路: 参考的网站连接:http://blog.csdn.net/wangjian8006/article/det ...

  2. Matrix Chain Multiplication[HDU1082]

    Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. UVA 442 二十 Matrix Chain Multiplication

    Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  4. 例题6-3 Matrix Chain Multiplication ,Uva 442

    这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...

  5. UVa442 Matrix Chain Multiplication

    // UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...

  6. UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)

    意甲冠军  由于矩阵乘法计算链表达的数量,需要的计算  后的电流等于行的矩阵的矩阵的列数  他们乘足够的人才  非法输出error 输入是严格合法的  即使仅仅有两个相乘也会用括号括起来  并且括号中 ...

  7. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

  8. UVA——442 Matrix Chain Multiplication

    442 Matrix Chain MultiplicationSuppose you have to evaluate an expression like A*B*C*D*E where A,B,C ...

  9. ACM学习历程——UVA442 Matrix Chain Multiplication(栈)

    Description   Matrix Chain Multiplication  Matrix Chain Multiplication  Suppose you have to evaluate ...

随机推荐

  1. 无法创建链接服务器 "xxx" 的 OLE DB 访问接口 "OraOLEDB.Oracle" 的实例。 (Microsoft SQL Server,错误: 7302)

    出现这个错误,有两个最常见的两个原因 1.注册表 <1>按下WIN+R,打开“运行”窗口,输入“regedit”,回车 <2>在打开的注册表编辑器的左侧按如下路径依次展开: H ...

  2. angular.js升序降序过滤器

    向指令添加过滤器 过滤器可以通过一个管道字符(|)和一个过滤器添加到指令中. orderBy 过滤器根据表达式排列数组: 例如: <div ng-app="myApp" ng ...

  3. JMM内存管理

    原文地址http://www.cnblogs.com/BangQ/p/4045954.html 原本准备把内存模型单独放到某一篇文章的某个章节里面讲解,后来查阅了国外很多文档才发现其实JVM内存模型的 ...

  4. 开机自动挂载 VHD 的方法

    一.批处理 除了将 VHD 文件用人工方式在[磁盘管理]里[附加]来挂载以外,也能用[脚本]来实现自动挂载. 打开[启动],将写好的 mount.bat 放入即可: Mount.bat 文件的内容为: ...

  5. lr_abort()、exit(-1) 和 return-1之间的区别

    int status; status = web_url("Login", "URL=https://secure.computing.com//login.asp?us ...

  6. Win10安装安卓ADB驱动

    Win10安装安装ADB驱动 Step1: 首先在黄色感叹号的ADB Interface 点右键菜单,选择“更新驱动程序软件”菜单. 在弹出“更新驱动程序软件”窗口中,选择下面一项“浏览计算机以查找驱 ...

  7. Django 过滤器

    过滤器 描述 示例 upper 以大写方式输出 {{ user.name | upper }} add 给value加上一个数值 {{ user.age | add:"5" }} ...

  8. magento寄存器的使用

    1.Mage::register('validation_image_name', $validationImageName);//这个是把变量$validationImageName存储在valid ...

  9. HDU 1312 Red and Black (DFS)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  10. 双击td字段,出现编辑文本框(更改之后发送数据请求) jsp

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head ...