noip 2005 等价表达式
/*
开始想的是 维护a的每个指数的系数
然而不好办 然而还有^10^10^10这种数据
特殊值带入吧 多搞几个素数 接下来就是玄学的事了
给a赋值之后 就是简单地表达式求值
虽然思路简单 但是字符串一向很恶心、、 数据括号有问题。。。。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 255
#define mod 10007
using namespace std;
int n,a,top1,top2,l,li,s1[maxn],s2[maxn];
int ans[]={,,,,,};
int target[];
int order[];
char s[maxn],si[maxn],c;
int Mi(int x,int y)
{
int ans=x;
for(int i=;i<=y;i++)
ans=(ans*x)%mod;
return ans;
}
int Go(int x,int y,char z)
{
if(z==)return (x-y+mod)%mod;
if(z==)return (x+y)%mod;
if(z==)return (x*y)%mod;
if(z==)return Mi(x,y);
}
int main()
{
order[]=order[]=;
order[]=;order[]=;
gets(si);li=strlen(si+);l=;
for(int i=;i<=li;i++)
if(si[i]==' ')continue;
else s[++l]=si[i];
s[]='(';s[++l]=')';
for(int k=;k<=;k++)
{
a=ans[k];int i=;top1=top2=;
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2));
while(i<=l)
{
if(s[i]=='a')
{
if(top1>=&&s[i-]>=''&&s[i-]<='')
{
int tmp=s1[top1];
s1[top1]=a*tmp%mod;
}
else top1=top1+,s1[top1]=a;
}
if(s[i]>=''&&s[i]<='')
{
int x=;
while(s[i]>=''&&s[i]<=''){x=x*+s[i]-'';i++;}
top1=top1+;s1[top1]=x;i=i-;
}
else
{
if(s[i]=='(')top2=top2+,s2[top2]=s[i];
if(s[i]=='^')
{
int x=;i=i+;int r=s1[top1];
while(s[i]>=''&&s[i]<=''){x=x*+s[i]-'';i++;}
s1[top1]=Go(r,x,'^');i=i-;
}
if(s[i]=='+'||s[i]=='-'||s[i]=='*')
{
if(order[s[i]]<=order[s2[top2]])
{
while(order[s[i]]<=order[s2[top2]])
{
int r1=s1[top1];top1=top1-;
int r2=s1[top1];top1=top1-;
int r3=Go(r2,r1,s2[top2]);
top2=top2-;top1=top1+;s1[top1]=r3;
}
top2=top2+;s2[top2]=s[i];
}
else if(order[s[i]]>order[s2[top2]])top2=top2+,s2[top2]=s[i];
}
if(s[i]==')')
{
while(s2[top2]!='('&&top2)
{
int r1=s1[top1];top1=top1-;
int r2=s1[top1];top1=top1-;
int r3=Go(r2,r1,s2[top2]);
top2=top2-;top1=top1+;s1[top1]=r3;
}
top2=top2-;
}
}
i=i+;
}
target[k]=s1[];
}
scanf("%d",&n);c=getchar();
for(int p=;p<=n;p++)
{
li=;memset(si,,sizeof(si));
gets(si);li=strlen(si+);l=;
for(int i=;i<=li;i++)
if(si[i]==' ')continue;
else s[++l]=si[i];
s[]='(';s[++l]=')';int sum=;
for(int k=;k<=;k++)
{
a=ans[k];int i=;top1=top2=;
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2));
while(i<=l)
{
if(s[i]=='a')
{
if(top1>=&&s[i-]>=''&&s[i-]<='')
{
int tmp=s1[top1];
s1[top1]=a*tmp%mod;
}
else top1=top1+,s1[top1]=a;
}
if(s[i]>=''&&s[i]<='')
{
int x=;
while(s[i]>=''&&s[i]<=''){x=x*+s[i]-'';i++;}
top1=top1+;s1[top1]=x;i=i-;
}
else
{
if(s[i]=='(')top2=top2+,s2[top2]=s[i];
if(s[i]=='^')
{
int x=;i=i+;int r=s1[top1];
while(s[i]>=''&&s[i]<=''){x=x*+s[i]-'';i++;}
s1[top1]=Go(r,x,'^');i=i-;
}
if(s[i]=='+'||s[i]=='-'||s[i]=='*')
{
if(order[s[i]]<=order[s2[top2]])
{
while(order[s[i]]<=order[s2[top2]])
{
int r1=s1[top1];top1=top1-;
int r2=s1[top1];top1=top1-;
int r3=Go(r2,r1,s2[top2]);
top2=top2-;top1=top1+;s1[top1]=r3;
}
top2=top2+;s2[top2]=s[i];
}
if(order[s[i]]>order[s2[top2]])top2=top2+,s2[top2]=s[i];
}
if(s[i]==')')
{
while(s2[top2]!='('&&top2)
{
int r1=s1[top1];top1=top1-;
int r2=s1[top1];top1=top1-;
int r3=Go(r2,r1,s2[top2]);
top2=top2-;top1=top1+;s1[top1]=r3;
}
top2=top2-;
}
}
i=i+;
}
if(s1[]==target[k])sum++;
}
if(sum==)printf("%c",'A'-+p);
}
return ;
}
noip 2005 等价表达式的更多相关文章
- NOIP 2005 等价表达式 (TYVJ P1060)
		
做题记录: 2016-08-10 23:35:09 背景 NOIP2005 提高组 第四道 描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代 ...
 - NOIP 2005 等价表达式 题解
		
题意 给一个表达式然后再给n个表达式,判断是否等价 一道大模拟题,将a带为数,并且取模防止溢出 #include<bits/stdc++.h> using namespace std; c ...
 - 等价表达式  2005年NOIP全国联赛提高组(栈模拟)
		
P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的 ...
 - 数据结构--栈 codevs 1107 等价表达式
		
codevs 1107 等价表达式 2005年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Descripti ...
 - 洛谷P1054 等价表达式
		
P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的 ...
 - 等价表达式(noip2005)
		
3.等价表达式 [问题描述] 兵兵班的同学都喜欢数学这一科目,中秋聚会这天,数学课代表给大家出了个有关代数表达式的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也 ...
 - 洛谷 P1054 等价表达式 解题报告
		
P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的 ...
 - 洛谷 P1054 等价表达式
		
洛谷 P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式, ...
 - 【Tyvj 1060】【NOIP 2005】等价表达式
		
设a为一个质数,模数为另一个质数,然后暴力算多项式的答案,如果答案相等就认为两个多项式相等. 这种hash有出错概率的题为什么还是要用hash呢?因为出错的概率实在太小了,a和模数的值取得好出题人根本 ...
 
随机推荐
- 利用js加载本地图片预览功能
			
直接上代码: 经测试,除safari6包括6以下不支持,其他均可正常显示. 原因:safari6不支持filereader,同时不能使用IE滤镜导致失效. fix: 可以利用canvas,解决safa ...
 - 学会用Clang来进行内存泄露分析
			
最近项目出现了内存泄露的问题,对于PC x86平台来说,一点点的内存泄露往往不会出错,很难进行debug调试.这个时候我们可以用到苹果给我们带来的神器--Clang编译器来进行内存泄露分析检测,开关打 ...
 - STL容器介绍
			
STL的容器可以分为以下几个大类: 一:序列容器, 有vector, list, deque, string. 二 : 关联容器, 有set, multiset, map, mulmap, h ...
 - oracle删除互相关联的记录
			
今天遇到一个问题,在数据库中删除一条记录,但是在删除的时候报错了,报出该记录已经被其他子记录引用,想了好久不知道怎么做,后来发现报错提示信息中会提示删除该记录时影响了那个约束条件,于是思路出来了: 1 ...
 - Smarty 模板引擎 fetch()和display()函数的区别?
			
Smarty模板函数里面有这样一个方法:fetch("template.htm"),他和display("template.htm");最大的不同就是fetch ...
 - Raspberry PI Model B+ (LCD显示CPU温度)
			
Title:Raspberry PI Model B+ (LCD显示CPU温度) --2015-01-29 17:44 买了块连接Raspberry PI Model B+的LCD显示器,上面没写C ...
 - OSSEC
			
[科普]入侵检测系统ossec配置文件详解 http://www.freebuf.com/articles/system/11862.html http://www.freebuf.com/autho ...
 - Qt for Android 开发大坑123
			
http://blog.csdn.net/qyvlik/article/details/50989685 http://blog.csdn.net/qyvlik/article/details/515 ...
 - BZOJ 1003  [ZJOI2006]物流运输trans
			
1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4242 Solved: 1765[Submit] ...
 - -_-#【Better Code】字符串匹配
			
提高 web 应用性能之 JavaScript 性能调优