ACM ICPC Asia Regional 2011 Kuala Lumpur C题
看了逆波兰表达式之后,发现真是强悍的数据结构,栈的应用怎么感觉一辈子也学不完了呢
后缀表达式即逆波兰表达式,就是将所有的运算符按照一定的等级全部都安排到数字的后面去,实现正确的运算法则。
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题的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- hduoj 4706 Children'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) ...
- 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 ...
- 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 ...
随机推荐
- bzoj4637:期望
思路:最小生成树计数只不过加了一个期望,由于期望具有线性性质,就可以转化为每条边的期望之和,那么一条边的期望如何求呢,在最小生成树记数中,是把相同边权的一起处理,之后把属于连通块内的点缩点,也就是说, ...
- 二叉搜索的各种bugs——重复递增序列
int binary_search(int* A, int value, int p, int r); int main(int argc, char *argv[]){ , , , , , , , ...
- 字符设备驱动、平台设备驱动、设备驱动模型、sysfs的比较和关联
转载自:http://www.kancloud.cn/yueqian_scut/emlinux/106829 学习Linux设备驱动开发的过程中自然会遇到字符设备驱动.平台设备驱动.设备驱动模型和sy ...
- mysql远程访问的权限
1.改表法 mysql>use mysql; mysql>update user set host = '%' where user = 'root'; 2.授权法 例如,你想myuser ...
- c#拖放
AllowDrop DragEnter: if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Cop ...
- memcached全面剖析--4
memcached的分布式算法 memcached的分布式 正如第1次中介绍的那样, memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能. 服务器端仅包括 第2次. 第 ...
- PHP生成订单号(产品号+年的后2位+月+日+订单号)
require '../common.inc.php'; /* * 产品号+年的后2位+月+日+订单数 * @param [Int] $prodcutId 产品号 * @param [Int] $tr ...
- css实现div块半透明
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ecshop会员中心增加订单搜索功能
在user.php中的act=order_list中增加以下程序. $order_sn = isset($_REQUEST['order_sn'])?$_REQUEST['order_sn']:''; ...
- Python设计模式——工厂方法模式(FactoryMethod)
需求:有一个学雷锋活动,有买米和扫地两个内容,参与的人有大学生和社区志愿者,他们各自的方法不一样. 如果用简单工厂模式实现: #encoding=utf-8 __author__ = 'kevinlu ...