因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下:

int Cal(){

if(括号)  sum += Cal();

   else sum += num;

  return sum;

}

  但是这个题目着实坑了我一下,见过WA了,没见过TLE呢……我因为没有看到有空格这个条件,无线TLE,又是消除函数又是改用数组模拟栈,其实就是读入出错和忘记了处理空格,改了之后,成功AC了。代码如下:

#include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
#define maxn 100
int pos[maxn],st[maxn];
int Cal(int id,const char *a){
int sum = ,tmp;
for(int i = id+;i < pos[id];i++){
if(a[i] == '('){
tmp = Cal(i,a);
if(a[i-]=='(' || a[i-]=='+') sum += tmp;
else if(a[i-]=='-') sum -= tmp;
else if(a[i-]=='*') sum *= tmp;
i = pos[i];
}
else if((a[i]>='a'&&a[i]<='z')||(a[i]>=''&&a[i]<='')){
if(a[i]>='a'&&a[i]<='z') tmp = (a[i]-'a'+);
else tmp = a[i]-'';
if(a[i-]=='(' || a[i-]=='+') sum += tmp;
else if(a[i-]=='-') sum -= tmp;
else if(a[i-]=='*') sum *= tmp;
i++;
}
}
return sum;
}
int getnum(const char *a){
memset(pos,,sizeof(pos));
memset(st,,sizeof(st));
int len = strlen(a);
int top = ;
for(int i = ;i < len;i++)
{
if(a[i]=='(') st[top++] = i;
if(a[i]==')') {
pos[st[--top]] = i;
}
}
int sum = ,tmp;
for(int i = ;i < len;i++){
if(a[i]=='('){
tmp = Cal(i,a);
if(i == || a[i-]=='+')
sum += tmp;
else if(a[i-] == '-') sum -= tmp;
else if(a[i-] == '*') sum *= tmp;
i = pos[i];
}
else if((a[i]>='a'&&a[i]<='z')||(a[i]>=''&&a[i]<='')){
if(a[i]>='a'&&a[i]<='z') tmp = (a[i]-'a'+);
else tmp = a[i]-'';
if(i == || a[i-]=='+')
sum += tmp;
else if(a[i-] == '-') sum -= tmp;
else sum *= tmp;
i++;
}
}
//printf("the num = %d\n",sum);
return sum;
}
int main()
{
int t,lena,lenb,num1,num2,tot;
char a[maxn],b[maxn];
scanf("%d",&t);
getchar();
while(t--){
gets(a);
lena = strlen(a);
tot = ;
for(int i = ;i < lena;i++){
if(a[i]==' ') continue;
else b[tot++] = a[i];
}
b[tot] = '\0';
num1 = getnum(b);
gets(a);
lena = strlen(a);
tot = ;
for(int i = ;i < lena;i++){
if(a[i]==' ') continue;
else b[tot++] = a[i];
}
b[tot] = '\0';
num2 = getnum(b);
if(num1 == num2) puts("YES");
else puts("NO");
}
return ;
}

UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)的更多相关文章

  1. 数据结构——POJ 1686 Lazy Math Instructor 栈的应用

    Description A math instructor is too lazy to grade a question in the exam papers in which students a ...

  2. POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑

    Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...

  3. Lazy Math Instructor

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3721   Accepted: 1290 Description A m ...

  4. poj 1684 Lazy Math Instructor(字符串)

    题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #includ ...

  5. POJ 1686 Lazy Math Instructor(栈)

    原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...

  6. 递归遍历嵌套结构(多层List)中的元素 ------Python

    读Python基础教程(第二版)后看到了这么一个东西,就是利用递归遍历嵌套结构中的元素. 上代码: #encoding:UTF-8 def flatten(nested): try: #不要迭代类似字 ...

  7. python 递归展开嵌套的序列(生成器用法)

    任何使用yield语句的函数都称为生成器.调用生成器函数将创建一个对象,该对象通过连续调用next()方法(在python3中是__next__())生成结果序列. next()调用使生成器函数一直运 ...

  8. Python之旅Day3 文件操作 函数(递归|匿名|嵌套|高阶)函数式编程 内置方法

    知识回顾 常见五大数据类型分类小结:数字.字符串.列表.元组.字典 按存值个数区分:容器类型(列表.字典.元组) 标量原子(数字.字符串) 按是否可变区分:可变(列表.字典) 不可变(数字.字符串.元 ...

  9. 递归、嵌套for循环、map集合方式实现树形结构菜单列表查询

    有时候, 我们需要用到菜单列表,但是怎么样去实现一个菜单列表的编写呢,这是一重要的问题. 比如我们需要编写一个树形结构的菜单,那么我们可以使用JQuery的zTree插件:http://www.tre ...

随机推荐

  1. Linux下好用的简单实用命令

    1.你是否为在输入了一大串命令之后发现第一个字符打错了而苦恼?只能删除重来嘛?或者一步步左移光标? NO,一个组合键轻松搞定 Ctrl+A -----到命令行首 Ctrl+E ------到命令行末 ...

  2. 27.编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish 和Dog,定义主类E,在其main方法中分别创建其对象并测试对象的特性。

    ///Animal类 package d922A; public class Animal { private String kind; public String getKind() { Syste ...

  3. 元素NULL判断

    元素取值val() val()方法主要用来获取form元素的值像input select textarea.在对select取值的时候当没有option被选定时val()会返回null,至少一个opt ...

  4. js移动端tap事件封装

    这几天做项目,发现移动端需要触摸事件,而click肯定是不行的,于是我对tap事件封装进行了搜索,找到了一篇文章,原文地址如下:http://www.jb51.net/article/50663.ht ...

  5. C Runtime Library来历, API, MFC, ATL关系

    首先说明,我google了半天,想找到英文的关于这个资料,但是实在找不到,只好转载国人的讨论. CRT原先是指Microsoft开发的C Runtime Library,用于操作系统的开发及运行.后来 ...

  6. 2.Add Two Numbers-两个单链表相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. 框架基础:ajax设计方案(一)---集成核心请求

    报告,我要说话!xp被历史淘汰了,IE6 say goodbye了,太TM开心了,从此不要兼容IE6了,哈哈哈哈哈哈 报告,我要说话!IE这sb为啥不早点被杀掉呢,找工作听说要兼容IE,立马软了,唉唉 ...

  8. Android测试日志文件抓取与分析

    1.log文件分类简介 实时打印的主要有:logcat main,logcat radio,logcat events,tcpdump,还有高通平台的还会有QXDM日志 状态信息的有:adb shel ...

  9. 利用DataImportHandler建索引时一直无法完成

    问题研究 项目中需要利用DataImportHandler从hive中sync数据到solr.发现有时候hive sql已经执行完几个小时了,sync任务还没有完成,貌似哪里卡住了.重启solr后重新 ...

  10. Hadoop概论

    1.Hadoop核心项目:HDFS(分布式文件系统)和MapReduce(并行计算框架) 2.HDFS的架构 主从结构 主节点,只有一个:namenode(接受用户操作要求:维护文件系统的目录结构:管 ...