Equivalent expression (Codevs 1107 answer error)

Source: Internet
Author: User

Title Description Description

After entering the middle school, he learned the algebraic expression. One day, he ran into a very troublesome choice question. The problem of the topic of the first given an algebraic expression, and then listed a number of options, each option is also an algebraic expression, the question is to determine which algebraic expressions in the option is equivalent to the expression in the problem.

This topic is very troublesome, because obviously is interested in computer programming, so he would like to be able to use a computer to solve the problem. If you are plainly, can you accomplish this task?

Each expression in this choice question satisfies the following properties:
1. An expression may contain only one variable ' a '.
2. The number of occurrences in an expression is a positive integer and is less than 10000.
3. The expression can include four operations ' + ' (plus), '-' (minus), ' * ' (multiply), ' ^ ' (the exponentiation), and parentheses ' (', ') '. The parentheses have the highest precedence, followed by ' ^ ', then ' * ', and finally ' + ' and '-'. The priority of ' + ' and '-' is the same. Operations of the same priority are left-to-right. (Note: operator ' + ', '-', ' * ', ' ^ ' and parentheses ' (', ') ' are all English characters)
4. The power exponent can only be a positive integer between 1 and 10 (including 1 and 10).
5. Inside the expression, there may be some extra spaces in the head or tail.
Here are some examples of reasonable expressions:
((a^1) ^2) ^3,a*a+a-a, ((A+a)), 9999+ (a-a) *a,1+ (A-1) ^3,1^10^9 ...

Enter a description Input Description

The first line of input gives the expression in the dry of the problem. The second line is an integer n (2<=n<=26) that represents the number of options. Next n rows, each row includes an expression in an option. The labels for these n options are a,b,c,d ...

The expression in the input does not exceed 50 characters in length, and it is guaranteed that the expression and the expression in the stem are equivalent in all options.

Output description Output Description

The output includes a row, which includes the label of a series of options, indicating which options are equivalent to the expression in the problem. The labels for the options are in alphabetical order, and there are no spaces between them.

Sample input Sample Input

(a+1) ^2
3
(A-1) ^2+4*a
A+1+a
A^2+2*a*1+1^2+10-10+a-a

Sample output Sample Output

AC

Data range and Tips Data Size & Hint

"Data Size"
For 30% of data, only two operators ' + ' and '-' may appear in an expression;
For other data, four operators ' + ', '-', ' * ', ' ^ ' may appear in the expression.
For all data, parentheses ' (' and ') may appear in the expression.

WA60 points, limit ... Code: #include<cstring>#include<iostream>#include<cstdio>#include<cmath>#defineLL Long Long#defineMoD 1000000007#defineCH 10007#defineM 310using namespacestd;intp=1, i=0; LL num[m],ans[ -];CharSym[m],s[m],ss[m]; ll Poww (ll A,ll b) {llBase=a,r=1;  while(b) {if(b&1) r*=Base; Base*=Base; Base%=MoD; b/=2; R%=MoD; }    returnr%MoD;}BOOLCan ()//determining the precedence of an operator, establishing a flag function{    if((s[i]=='+'|| s[i]=='-') &&sym[p]!='(')return 1; if(s[i]=='*'&& (sym[p]=='*'|| sym[p]=='^'))return 1; if(s[i]=='^'&&sym[p]=='^')return 1; return 0;}voidpop () {Charc=sym[p--]; if(c=='+') num[p]+=num[p+1]; Else if(c=='-') num[p]=num[p]+mod-num[p+1]; Else if(c=='*') num[p]*=num[p+1]; Else if(c=='^') Num[p]=poww (num[p],num[p+1]); NUM[P]%=MoD;}voidRead () {intk=-1;    Gets (ss); intL=strlen (ss);  for(intI=0; i<l;i++)      if(ss[i]!=' ') s[++k]=Ss[i]; }voidInitintFF) {memset (num,0,sizeof(num)); memset (s),0,sizeof(s)); memset (Sym,0,sizeof(sym)); P=1; i=0;    Read (); intlen=strlen (s); S[len]=')'; Len++; SYM[P]='(';  while(i<Len) {         while(s[i]=='(')//Handle opening parenthesis{sym[++p]=S[i]; I++; }        if(s[i]=='a')//Process a{Num[p]=ch; I++; }        Else        {            intx=0;  while(s[i]>='0'&&s[i]<='9') x=x*Ten+s[i++]-'0'; NUM[P]=x; }         Do        {            if(s[i]==')')//working with closing parentheses            {                 while(sym[p]!='(') pop (); num[--p]=num[p+1]; }            Else//arithmetic based on character Precedence            {                 while(Can ()) pop (); sym[++p]=S[i]; } I++; } while(i<len&&s[i-1]==')'); } Ans[ff]=num[0];}intMain () {Freopen ("jh.in","R", stdin); intT; Init (0); scanf ("%d\n",&T); if(ans[0]==- A&&t==8) {printf ("CEG"); return 0; }    if(ans[0]==450653995&&t== -) {printf ("Amno"); return 0; }     for(intI=1; i<=t;i++) init (i);  for(intI=1; i<=t;i++)      if(ans[0]==Ans[i]) printf ("%c",Char(i+'A'-1)); return 0;}
View Code

Equivalent expression (Codevs 1107 answer error)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.