/*
开始想的是 维护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 等价表达式的更多相关文章

  1. NOIP 2005 等价表达式 (TYVJ P1060)

    做题记录: 2016-08-10 23:35:09 背景 NOIP2005 提高组 第四道 描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代 ...

  2. NOIP 2005 等价表达式 题解

    题意 给一个表达式然后再给n个表达式,判断是否等价 一道大模拟题,将a带为数,并且取模防止溢出 #include<bits/stdc++.h> using namespace std; c ...

  3. 等价表达式 2005年NOIP全国联赛提高组(栈模拟)

    P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的 ...

  4. 数据结构--栈 codevs 1107 等价表达式

    codevs 1107 等价表达式 2005年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Descripti ...

  5. 洛谷P1054 等价表达式

    P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的 ...

  6. 等价表达式(noip2005)

    3.等价表达式 [问题描述]    兵兵班的同学都喜欢数学这一科目,中秋聚会这天,数学课代表给大家出了个有关代数表达式的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也 ...

  7. 洛谷 P1054 等价表达式 解题报告

    P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的 ...

  8. 洛谷 P1054 等价表达式

    洛谷 P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式, ...

  9. 【Tyvj 1060】【NOIP 2005】等价表达式

    设a为一个质数,模数为另一个质数,然后暴力算多项式的答案,如果答案相等就认为两个多项式相等. 这种hash有出错概率的题为什么还是要用hash呢?因为出错的概率实在太小了,a和模数的值取得好出题人根本 ...

随机推荐

  1. python之map和filter

    li = [11,22,33,44,55,66] ret = filter(lambda a:a>33,li) print(list(ret)) ret2 = map(lambda a:a+10 ...

  2. .NET MVC插件化开发框架源码(插件功能完善版)

    离上次第一次上传源码一个多星期了,在工作之余今天终于把插件管理部门的功能全部完善了,已可用于实际开发,管理界面因为没人帮忙设计,所以有点丑,今天这版算是0.1.0.0吧,后面我会发布很多插件来填充这个 ...

  3. ES聚合实例

    在es中需要根据app_categor进行聚合,JSON查询语句如下: { "query": { "bool": { "must": [ { ...

  4. js渲染引擎 tempo.js

    1.引入tempo.js <script src="js/tempo.js" type="text/javascript"></script& ...

  5. 转:nginx防DDOS攻击的简单配置

    文章来自于: 近期由于工作需要,做了些防DDOS攻击的研究,发现nginx本身就有这方面的模块ngx_http_limit_req_module和ngx_http_limit_conn_module. ...

  6. Jest

    http://www.ibm.com/developerworks/cn/java/j-javadev2-24/

  7. 多线程 Worker Thread 模式

    Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来. Work ...

  8. centos ppp拨号

    CentOS 6.4 电信ADSL 拨号连接的配置,亲测 .下载 rp-pppoe-3.8.tar.gz http://down1.chinaunix.net/distfiles/rp-pppoe-3 ...

  9. Linux&shell之结构化命令进阶

    写在前面:案例.常用.归类.解释说明.(By Jim) for命令重复一系列的命令是一种常见的编程实践. #!/bin/bash # basic for command for test in A B ...

  10. "红色病毒"问题 HDU 2065 递推+找循环节

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=2065 递推类题目, 可以考虑用数学方法来做, 但是明显也可以有递推思维来理解. 递推的话基本就是状态 ...