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

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

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. C. Sonya and Queries

    http://codeforces.com/contest/714/problem/C 看到这题目,想想,肯定不能暴力啊,如果用map,如何快速找到满足要求的数目,然后,长度18,我想,这不是熟悉的t ...

  2. OpenJudge 2757 最长上升子序列 / Poj 2533 Longest Ordered Subsequence

    1.链接地址: http://poj.org/problem?id=2533 http://bailian.openjudge.cn/practice/2757 2.题目: 总Time Limit: ...

  3. json字符串转JSONObject,输出JSONObject问题

    json架包:json-lib-2.4-jdk15.jar json字符串(存在null值)转JSONObject 后return JSONObject对象的时候会报错 例如: String str= ...

  4. Linux C 程序 数组(EIGHT)

    数组 1.一维数组的定义和使用,声明时数组默认值为0 int a[n]; 这样定义不合法,n是变量 ,数组规定[]里只能为常量 ] = {,,,,,,,,,}; a[] = {,} ;//部分赋值 , ...

  5. Android开发系列之SQLite

    上篇博客提到过SQLite,它是嵌入式数据库,由于其轻巧但功能强大,被广泛的用于嵌入式设备当中.后来在智能手机.平板流行之后,它作为文件型数据库,几乎成为了智能设备单机数据库的必选,可以随着安卓app ...

  6. WPF中实现根据拼音查找汉字

    1.WPF的一个触摸屏项目,需要输入姓名,但是屏幕不支持汉字输入,使用虚拟键盘不稳定,为了解决该问题特此进行处理. 2.新建一个类转换类,里面初始化一个数组,数组包含拼音,以及拼音下的常用的汉字. 3 ...

  7. ko list and css gradient

    <!DOCTYPE html> <html> <head> <title></title> <script src="js/ ...

  8. Automotive Security的一些资料和心得(6):AUTOSAR

    1.1 Introduction AUTOSAR(汽车开放系统架构)是一个开放的,标准化的汽车软件架构,由汽车制造商,供应商和开发工具共同开发.它联合了汽车OEM ,供应商和开发工具供应商,其目标是创 ...

  9. 使用WampServer 3.0

    在server上安装了WampServer 发现本地使用良好,但是无法从别的PC访问. 原因有二: 1.现象:输入连接无反应 原因:server本身用了80端口,所有WampServer我就设置了80 ...

  10. SecureCRT+WinSCP 共用 key pub 密钥 转换 ppk 登录ssh

    使用SecureCRT生成的密钥,无法在WinSCP使用, 使用puttygen.exe无法直接转换,解决办法 1.使用大于等于SecureCRT6.5版本,来转换 记得放入私钥,不是pub公钥.然后 ...