看了逆波兰表达式之后,发现真是强悍的数据结构,栈的应用怎么感觉一辈子也学不完了呢

后缀表达式即逆波兰表达式,就是将所有的运算符按照一定的等级全部都安排到数字的后面去,实现正确的运算法则。

OK,代码要自己好好看,理解了自然就很简单。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<time.h>
using namespace std;
int const N = ;
int hash[N];
char exp1[N],exp2[N],a1[N],a2[N],stack1[N];
int stack2[N];
int cal(char exp[],char a[],int len,char stack[])
{
int topa=,tops=;
for(int i=;i<len;i++)
{
if((exp[i]<=''&&exp[i]>='')||(exp[i]<='Z'&&exp[i]>='A')||(exp[i]<='z'&&exp[i]>='a'))
a[topa++]=exp[i];
else
{
if(exp[i]=='+'||exp[i]=='-')
{
while(tops>&&stack[tops-]!='(')
a[topa++]=stack[--tops];
stack[tops++]=exp[i];
}
else
if(exp[i]=='*')
{
while(tops>&&stack[tops-]!='('&&stack[tops-]!='+'&&stack[tops-]!='-')
a[topa++]=stack[--tops];
stack[tops++]=exp[i];
}
else
if(exp[i]==')')
{
while(stack[tops-]!='(')
a[topa++]=stack[--tops];
tops--;
}
else
if(exp[i]=='(')
stack[tops++]=exp[i];
}
}
while(tops>)
a[topa++]=stack[--tops];
return topa;
}
int Count(char a[],int len,int stack[])
{
int tops=;
for(int i=;i<len;i++)
{
if(a[i]>=''&&a[i]<='')
stack[tops++]=a[i]-'';
else
if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z'))
stack[tops++]=hash[a[i]];
else
{
if(a[i]=='*')
stack[tops-]=stack[tops-]*stack[tops-];
else
if(a[i]=='-')
stack[tops-]=stack[tops-]-stack[tops-];
else
if(a[i]=='+')
stack[tops-]=stack[tops-]+stack[tops-];
tops--;
}
}
return stack[];
}
int main()
{
srand(time());
int T;
cin>>T;
gets(exp1);
while(T--)
{
gets(exp1);
gets(exp2);
int len1=strlen(exp1);
int len2=strlen(exp2);
len1=cal(exp1,a1,len1,stack1);
len2=cal(exp2,a2,len2,stack1);
int f=;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
hash[j]=rand()%;
int ans1=Count(a1,len1,stack2);
int ans2=Count(a2,len2,stack2);
if(ans1!=ans2)
{
f=;break;
}
}
if(f)printf("YES\n");
else printf("NO\n");
}
return ;
}

ACM ICPC Asia Regional 2011 Kuala Lumpur C题的更多相关文章

  1. hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...

  2. hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...

  3. hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...

  4. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  5. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

  6. hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  7. hduoj 4706 Children&#39;s Day 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4706 Children's Day Time Limit: 2000/1000 MS (Java/Others) ...

  8. 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

    hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  9. 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

    I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. 九度OJ 1501 最大连续子序列乘积 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1501 题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 输入: 输入可能包含 ...

  2. AD,Group

    DataTable dtUser = GetEmptyDT(); Dictionary<DirectoryEntry, string> test1 = GetUserAndGroup(cl ...

  3. aspx页面状态管理(查询字符串Request与Application)

    1,Request:可以方便的将信息从一个页面传递到另一个页面,通过url传递,不安全,数据量小,只能通过http-get提交的才可以 2,Application对象:()本质上是Hash表)所有访问 ...

  4. 解决ie8下h5元素兼容性的问题

    HTML5的语义化标签以及属性,可以让开发者非常方便地实现清晰的web页面布局,加上CSS3的效果渲染,快速建立丰富灵活的web页面显得非常简单. HTML5的新标签元素有: <header&g ...

  5. smarty模板的基础搭建

    1.下载smarty模板,官方即有 2.解压即可.你会得这样的目录 除了libs文件夹其余都可以删掉(不知道有啥用). 3.在当前目录下分别创建templates.templates_c.cache. ...

  6. 【BZOJ】3309: DZY Loves Math 莫比乌斯反演优化

    3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...

  7. Bootstrap优秀网站:乐窝网

    Bootstrap优秀网站:乐窝网 调用谷歌在线地图的API和Bootstrap工具包实现了租房和出租的一个平台. 佩服之极,09年跟一个哥们聊天时,他就提议过这方面的应用,终于看到有人实现了,祝贺. ...

  8. Kinetic使用注意点--blob

    new Blob(config) 参数: config:包含所有配置项的对象. { points: "存放路径点的数组,可以用一层数组[a,b,c,d].二层数组[[a,b],[c,d]]或 ...

  9. 安装使用adobe_photoshop_cs6

    1.先断开网络,安装官方原版PS程序. 2.在安装程序界面选择“试用”安装. 3.等安装完成后,退出程序. 4.复制破解补丁到安装路径覆盖,如:D:\Program Files\Adobe\Adobe ...

  10. online web design tool

    https://webflow.com/ http://css-tricks.com/snippets/ https://www.fluidui.com/demos