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和模数的值取得好出题人根本 ...
随机推荐
- 初涉JavaScript模式 (12) : 沙箱模式
引子 上一篇说了模块模式,而对于其中的命名空间模式其实也是有着一些问题,比如每添加一个模块或则深入叠加都会导致长命名,并且对于多个库的不同版本同时运行,一不小心就会污染全局标识,而这两天也发现了JSe ...
- java 获取页面中的 a 标签 的 href 实例
Pattern p = Pattern.compile("<a\\s+href\\s*=\\s*(\"|\')?(.*?)[\"|\'|>]", P ...
- MySql存储过程—3、变量
1.变量的定义 在Mysql里面可以像我们写代码中一样定义变量来保持中间结果,看下面的格式: DECLARE variable_name datatype(size) DEFAULT default_ ...
- php防止SQL注入详解及防范
SQL 注入是PHP应用中最常见的漏洞之一.事实上令人惊奇的是,开发者要同时犯两个错误才会引发一个SQL注入漏洞一个是没有对输入的数据进行过滤(过滤输入),还有一个是没有对发送到数据库的数据进行转义( ...
- Retrofit2.0+OkHttp设置统一的请求头(request headers)
有时候要求Retrofit2的接口中每个都要增加上headers,又不想做重复的事情,可以使用这种方法来为每个request请求都设置上相同的请求头header. 修改请求头request heade ...
- 使用C#的自定义事件
public class CarDealer { public Action<string> NewCarInfo; //使用系统定义的泛型委托 public void NewCarCom ...
- UIAlertView的使用方法
UIAlertView的使用方法 1. 最简单的用法 UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" mes ...
- 7月12日至芯FPGA就业班招生
至芯科技FPGA就业培训班将于2014年7月12日正式开班,届时至芯科技将秉承着一贯的教学传统,为广大师生打造一个专业的技术平台和轻松的学习环境. 在夏宇闻教授的带领下,至芯科技汇聚了一批来自国内外大 ...
- 使用Protractor进行AngularJS e2e测试案例
环境: y@y:karma-t01$ protractor --version Version y@y:karma-t01$ node -v v4.2.2 y@y:karma-t01$ y@y:kar ...
- 电源VCC、VSS、VDD、VEE、VPP、Vddf标号的区别
一.解释VCC:C=circuit表示电路的意思,即接入电路的电压: VDD:D=device表示器件的意思,即器件内部的工作电压:VEE:发射极电源电压,EmitterVoltage,一般用于ECL ...