/*
开始想的是 维护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. 对于HttpContext.Current的一点理解

    string[] userInfomationSplits = HttpContext.Current.User.Identity.Name.Split(new string[] { "\\ ...

  2. [Git]Git安装

    1.什么是Git Git是一个分布式版本控制/软件配置管理软件, git是用于Linux内核开发的版本控制工具, 与CVS.Subversion一类的集中式版本控制工具不同,它采用了分布式版本库的作法 ...

  3. iOS开发——OC篇&纯代码退出键盘

    关于iOS开发中键盘的退出,其实方法有很多中,而且笔者也也学会了不少,包括各种非纯代码界面的退出. 但是最近开始着手项目的时候却闷了,因为太多了,笔者确实知道有很多中方法能实现,而且令我影响最深的就是 ...

  4. codeforces 573C Bear and Drawing

    Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but ...

  5. 转:玩转Google开源C++单元测试框架Google Test系列

    转自http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Google的开源C++单元测试框架Google ...

  6. C++学习笔记--Season 2

    一个简单的EGE程序: #include "graphics.h" //EGE库的头文件 int main(int argc, char** argv) { initgraph(, ...

  7. Matrix(线段树版)

    poj2155:http://poj.org/problem?id=2155 题意;同上一遍随笔. 题解:这里用二维线段树打了一发.第一次学习别人的代码.才学的.这种树套树的程序,确实很费脑子,一不小 ...

  8. [Android] 获取音频输出getOutput

    每创建一个AudioTrack,代表需要新增一个输出实例,即需要根据音频流的的stream type,音频流的音轨数量,采样率,位宽等数据来重新构建buffer,而且输出的设备也可能会有变化,由于An ...

  9. 基于MINA框架快速开发网络应用程序

    1.MINA框架简介 MINA(Multipurpose Infrastructure for Network Applications)是用于开发高性能和高可用性的网络应用程序的基础框架.通过使用M ...

  10. static用法一

    #include "stdafx.h" #include "string.h" struct student { int num; ]; char sex; } ...