Https://vijos.org/p/1765

The method of using the White Book is n ^ 2, so I have a point .. Sad.

I optimized it a little.

This question gives me the biggest feeling, not the question itself, but the feeling that after I think about it, I seriously find the wrong one and submit it for such a high score.

Be sure to answer questions carefully and carefully.

The most important thing to write these questions is to be careful. If you fail to handle some details, you will get down and fall into the quagmire of debugging.

Think about the program I wrote before. Which one is not like this?

First of all, you must have a rough framework and I have repeatedly demonstrated that this is correct (without considering the full score, it can act as a brute force to detect and cheat points)

Be sure to repeat the arguments!

Then, you must concentrate on writing your own data and follow the framework. If you find that you are thinking wrong, you must first leave the keyboard and think twice carefully.

After writing a huge amount of code, you must perform static error checking. There are definitely several issues: variable name, array, logic, and SB errors. You must verify the logic correctness of your code repeatedly.

Before testing the sample.

It is usually direct.

If this is not the case, do not go into the debugging Quagmire. Check again where errors occur and where errors may occur. Then the corresponding debugging is done there, and the corresponding debugging is dependent on the chunking of your code. If the chunking is good, debugging is easy.

Okay ..

In the future, don't worry. You need to know that this is your battle, just to solve the problem, instead of simply pursuing the beautiful code, but correctness.

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <map>#include <cassert>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }map<string, int> mp;typedef long long ll;const int N=105, M=100005;const ll eps=1e9;int n, m, ID, a[N], flag, tot, db;char s[15];struct node { node *l, *r; bool f, opt; int s; node() { l=r=NULL; f=opt=0; s=0; } }*root=NULL;struct dat { int s; bool f, opt; dat() { s=0; f=opt=0; } }b[M];int getid(char *st, int len) {string s(st, len);if(mp[s]) return mp[s];mp[s]=++ID;return ID;}ll cal(node *x) {if(x==NULL || flag) return 0;if(!x->opt) {if(x->f) return a[x->s];return x->s;}ll l=cal(x->l), r=cal(x->r), ret=0;int opt=x->s;if(flag) return 0;if(opt==‘/‘ && r==0) { flag=1; return 0; }if(opt==‘+‘) ret=l+r;if(opt==‘-‘) ret=l-r;if(opt==‘*‘) ret=l*r;if(opt==‘/‘) ret=l/r;if(abs(ret)>eps) { flag=1; return 0; }return ret;}void build(int l, int r, node *&x) {// if(db>1) return;// ++db;// assert(l<=r);if(l==r) {x=new node();x->f=b[l].f;x->s=b[l].s;return;}int p=0, c1=-1, c2=-1;for1(i, l, r) {if(!b[i].opt) continue;if(b[i].s==‘(‘) ++p;else if(b[i].s==‘)‘) --p;else if(!p && (b[i].s==‘+‘ || b[i].s==‘-‘)) c1=i;else if(!p && (b[i].s==‘*‘ || b[i].s==‘/‘)) c2=i;}if(c1==-1) c1=c2;if(c1==-1) { build(l+1, r-1, x); return; }//puts("here"); dbg(c1);x=new node();build(l, c1-1, x->l); build(c1+1, r, x->r);x->opt=1;x->s=b[c1].s;}int main() {read(n); read(m);for1(i, 1, n) {scanf("%s", s);getid(s, strlen(s));}char c=getchar(); while(c==‘\n‘||c==‘\r‘||c==‘ ‘) c=getchar();while(c!=‘\n‘) {if(c>=‘0‘ && c<=‘9‘) {int r=0;while(c>=‘0‘ && c<=‘9‘) r=r*10+c-‘0‘, c=getchar();b[++tot].s=r;}else if((c>=‘A‘ && c<=‘Z‘) || (c>=‘a‘ && c<=‘z‘)) {int len=0;while((c>=‘A‘ && c<=‘Z‘) || (c>=‘a‘ && c<=‘z‘)) s[len++]=c, c=getchar();s[len]=‘\0‘;b[++tot].s=getid(s, len);b[tot].f=1;}else if(c!=‘\n‘) {b[++tot].opt=1;b[tot].s=c;c=getchar();}}build(1, tot, root);while(m--) {for1(i, 1, n) read(a[i]);flag=0;int ans=cal(root);if(flag) puts("Error!");else printf("%d\n", ans);}return 0;}

Description

A calculator is a commonly used computing tool in life. It can be used to calculate various algebraic values. However, it has some disadvantages. One major problem is that it only supports constant operations. For an algebraic expression of the same form, you only need to re-input the constants because of their differences.

Programmers can evaluate using expressions in advanced languages. In PASCAL/C ++, expressions are a basic concept. Variables, constants, operators, Parentheses, and various functions can appear in expressions.

Here we only consider this expression: it consists of 52 uppercase/lowercase English letters, numbers '0 '~ '9', and '(,)', '+', '-', '*', and. Each operator only performs the binary operator function, that is, both the left and right sides of the operator must be computable. In particular, the '/' symbol is used to indicate division. A continuous English letter represents the variable name, and the variable name is only composed of letters. To simplify the problem, constants in the expression are natural numbers whose absolute values do not exceed 10 ^ 9, and no leading 0 exists. The expression calculation method is the same as that in the source program. That is, parentheses have the highest priority, while the '*' and '/' operators have higher priority than the '+' and '-' operators, the same priority is calculated from left to right in order.

Your task is to implement its computing function for a given expression. We will declare n variables and reference them in the expression. Then the T group data is given. Each group of data is a set of values of N variables. You need to calculate the return value of the expression for each group of data.

Format input format

The first line of the input file contains two integers n, T, indicating the number of variables referenced in the expression in turn, and the number of value groups to be calculated for n variables.

In the next n rows, each row contains a string consisting of letters, indicating each variable name in turn.

The next line contains a string that represents the content of the expression.

In the next T line, N integers in each line represent the values of each variable in turn.

Output Format

The output file contains Line T, and line I indicates the answer to the expression when n variables are used to obtain the value of group I entered.

If an integer is divided by 0 during the operation, or the absolute value of the intermediate result is greater than 10 ^ 9, an error is returned !"; Otherwise, an integer is output, indicating the operation result. The intermediate result of an operation refers to each value that is obtained from left to right in order of priority.

Example 1 input 1 [copy]

2 3xtotal1 + (total/x) 1 32 1550 0

Sample output 1 [copy]

478 error!

Restrictions

1 s

Prompt

20% of the data is satisfied, and there is no parentheses in the expression.

In addition, 20% of the data is satisfied, and there is no addition or subtraction operation in the expression.

In addition, 20% of the data is satisfied, and there is no multiplication or division operation in the expression.

80% of the data (including the above) is satisfied, and the length of the expression cannot exceed 100.

100% of the data is satisfied. 1 <= n <= 100,000, 1 <= T <= 10, the variable name length cannot exceed 10, and the expression length cannot exceed. The value of a variable and the constant in the expression are the natural numbers whose absolute values do not exceed 10 ^ 9. All variable names are different.

[Vijos] 1765 caculator (CODE)