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

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

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. 只有PD号的调起

    cicsterm-> CECI -> LI-> P -> TestQuery

  2. AbstractExecutorService (未完成)

    AbstractExecutorService是一个实现了ExecutorService的抽象类.主要实现了ExecutorService的invoke方法.

  3. [转]优化PHP程序的方法

    1. If a method c++an be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法可静态化 ...

  4. PC软件-实用工具 True Launch Bar

    True Launch Bar 官网 增强及自定义window任务栏快捷方式管理. 有免费版的Free Launch Bar 官网

  5. windows的路由例子

    一.不同网卡不同网关 wlan:192.168.100.1/24 wifi:192.168.8.1/24 1.如果想要特定的ip访问wifi网络 可以用 route add 1.2.3.4 mask ...

  6. ASP.NET输入文本框自动提示功能

    在ASP.NET Web开发中会经常用到自动提示功能,比如百度搜索.我们只要输入相应的关键字,就可以自动得到相似搜索关键字的提示,方便我们快速的输入关键字进行查询. 那么在ASP.NET中,如果我们需 ...

  7. HttpWebRequest中的KeepAlive

    一直不是非常理解.NET中HttpWebRequest的KeepAlive属性有何用处,看了这篇文章就清楚了! http://www.cnblogs.com/lwzz/archive/2011/08/ ...

  8. [Linux]root默认密码问题

    Linux/Ubuntu root默认密码问题   Ubuntu安装后root是没有默认的密码的,因为你还没给root设置密码,你第一个 user 是在 admin 组,他可以给 root 设置密码, ...

  9. php中的数组定义和使用

    <?php //这个是一个php关于数组的例子,简要的说明了数组的基本使用 //定义一个字符串数组 $fruit = array(\"apple\",\"orang ...

  10. thinkphp 框架的学习(1) 扩展配置文件

    在config.php里面写入 1:'LOAD_EXT_CONFIG' => array('SETTINGS' => 'settings'); 系统会判断是否有参数:LOAD_EXT_CO ...