NOIP 2005 等价表达式
来源:互联网 发布:单片机支持snmp协议 编辑:程序博客网 时间:2024/05/16 05:33
题目描述
明明进了中学之后,学到了代数表达式。有一天,他碰到一个很麻烦的选择题。这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的要求是判断选项中哪些代数表达式是和题干中的表达式等价的。
这个题目手算很麻烦,因为明明对计算机编程很感兴趣,所以他想是不是可以用计算机来解决这个问题。假设你是明明,能完成这个任务吗?
这个选择题中的每个表达式都满足下面的性质:
1. 表达式只可能包含一个变量‘a’。
2. 表达式中出现的数都是正整数,而且都小于10000。
3. 表达式中可以包括四种运算‘+’(加),‘-’(减),‘’(乘),‘^’(乘幂),以及小括号‘(’,‘)’。小括号的优先级最高,其次是‘^’,然后是‘’,最后是‘+’和‘-’。‘+’和‘-’的优先级是相同的。相同优先级的运算从左到右进行。(注意:运算符‘+’,‘-’,‘*’,‘^’以及小括号‘(’,‘)’都是英文字符)
4. 幂指数只可能是1到10之间的正整数(包括1和10)。
5. 表达式内部,头部或者尾部都可能有一些多余的空格。
下面是一些合理的表达式的例子:
((a^1) ^ 2)^3,a*a+a-a,((a+a)),9999+(a-a)*a,1 + (a -1)^3,1^10^9……
输入输出格式
输入格式:
输入文件equal.in的第一行给出的是题干中的表达式。第二行是一个整数n(2 <= n <= 26),表示选项的个数。后面n行,每行包括一个选项中的表达式。这n个选项的标号分别是A,B,C,D……
输入中的表达式的长度都不超过50个字符,而且保证选项中总有表达式和题干中的表达式是等价的。
输出格式:
输出文件equal.out包括一行,这一行包括一系列选项的标号,表示哪些选项是和题干中的表达式等价的。选项的标号按照字母顺序排列,而且之间没有空格。
输入输出样例
输入样例#1:
( a + 1) ^2
3
(a-1)^2+4*a
a + 1+ a
a^2 + 2 * a * 1 + 1^2 + 10 -10 +a -a
输出样例#1:
AC
说明
对于30%的数据,表达式中只可能出现两种运算符‘+’和‘-’;
对于其它的数据,四种运算符‘+’,‘-’,‘*’,‘^’在表达式中都可能出现。
对于全部的数据,表达式中都可能出现小括号‘(’和‘)’。
2005年提高组第四题
【分析】
去模拟吧…无力吐槽
tyvj数据有点不靠谱啊…
【代码】
//NOIP 等价表达式 #include<cmath>#include<stack>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define ll long long#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const ll mod=1e6+7;ll pri[1000]; //优先级,优先级越高越先进行运算 stack <ll> num,ope;string map[50];ll n,ans[12];inline ll ksm(ll x,ll y) //快速幂取模 { if(y==1) return x; ll k=ksm(x,y/2); if(y & 1) return (k*k*x)%mod; return (k*k)%mod;}int main(){ ll i,j,k; pri['(']=pri[')']=4; pri['^']=3; pri['*']=2; pri['+']=pri['-']=1; char ch=getchar(); while(ch=='\n' || ch=='\r') ch=getchar(); while(ch!='\n' && ch!='\r') { if(ch!=' ') map[0]+=ch; ch=getchar(); } scanf("%lld",&n); fo(i,1,n) { while(ch=='\n' || ch=='\r') ch=getchar(); while(ch!='\n' && ch!='\r') { if(ch!=' ') map[i]+=ch; ch=getchar(); } } //由于洛谷不支持getline,只能用快速读入的诡异方法... fo(j,0,n) { string str=map[j];ll len=str.size()-1; fo(k,1,10) { while(!num.empty()) num.pop(); while(!ope.empty()) ope.pop(); fo(i,0,len) { if(str[i]=='a') num.push(k); //a else if(str[i]>='0' && str[i]<='9') //数字 { ll tmp=0; while(str[i]>='0' && str[i]<='9') tmp=tmp*10+str[i]-'0',i++; num.push(tmp); i--; } else if(str[i]==')') //弹弹弹 { bool flag=1; if(ope.empty()) break; while(ope.top()!=(ll)'(') { ch=ope.top();ope.pop(); ll shu1=num.top();num.pop();ll shu2=num.top();num.pop(); if(ch=='*') num.push((shu2*shu1)%mod); if(ch=='+') num.push((shu2+shu1)%mod); if(ch=='-') num.push((shu2-shu1)%mod); if(ch=='^') num.push(ksm(shu2,shu1)); if(ope.empty()) break; } if(ope.empty()) break; ope.pop(); } else if(ope.empty()) ope.push(str[i]); else if(str[i]=='(') ope.push(str[i]); else { ch=ope.top(); if(ch=='(') { ope.push(str[i]); continue; } while(1) { if(ope.empty()) break; ch=ope.top(); if(pri[ch]<pri[str[i]] || ch=='(') break; ope.pop(); ll shu1=num.top();num.pop();ll shu2=num.top();num.pop(); if(ch=='*') num.push((shu2*shu1)%mod); if(ch=='+') num.push((shu2+shu1)%mod); if(ch=='-') num.push((shu2-shu1)%mod); if(ch=='^') num.push(ksm(shu2,shu1)); } ope.push(str[i]); } } while(!ope.empty()) { while(1) { if(ope.empty()) break; ch=ope.top();if(ch!=')') break;ope.pop(); if(ch==')' && ope.top()=='(') ope.pop(); else break; } if(ope.empty() || num.size()==1) break; ll shu1=num.top();num.pop();ll shu2=num.top();num.pop(); ch=ope.top();ope.pop(); if(ch=='*') num.push((shu2*shu1)%mod); if(ch=='+') num.push((shu2+shu1)%mod); if(ch=='-') num.push((shu2-shu1)%mod); if(ch=='^') num.push(ksm(shu2,shu1)); } int tt=num.top();while(tt<0) tt+=mod; if(j && ans[k]!=tt) break; if(j==0) {ans[k]=num.top();while(ans[k]<0) ans[k]+=mod;} } if(j && k==11) printf("%c",j+'A'-1); } printf("\n"); return 0;}
- NOIP 2005 等价表达式
- NOIP 2005 等价表达式
- NOIP 2005 等价表达式 四则运算
- 【NOIP 2005】 等价表达式 题解
- [noip 2005] 等价表达式(equal)
- noip 2005 T4 等价表达式
- NOIP 2005 等价表达式(hash算法)
- noip的等价表达式
- 等价表达式 2005年NOIP全国联赛提高组
- BSOJ1054 洛谷P1054 CODEVS1107【NOIP 2005提高】等价表达式 随机化
- [Codevs 1107][NOIP 1107]等价表达式
- 等价表达式
- 等价表达式
- 等价表达式
- 等价表达式
- 等价表达式
- sicily 1149 等价表达式
- Sicily 1149. 等价表达式
- lua笔记3,常用函数
- jsp访问数据库(完整总结)
- Spring小白到菜鸟修炼之基本概念(2)
- 大连ICPC总结【滚粗记】
- <学习笔记2>数据挖掘原理与运用-sql server2008 数据库
- NOIP 2005 等价表达式
- CNN学习笔记(1)稀疏连接和权值共享的理解
- 坚持与源动力
- 自定义仿ViewParger无限循环滑动界面
- HDU 5904 LCIS(dp)
- 客户端 用不用 bind 的区别
- HTML头
- Qt: 网络编程之UDP(理论+实例)
- 第三天学习数据结构,顺序栈