One book brush questions

Posted by Dysan on Tue, 18 Jan 2022 17:20:29 +0100

All in one book
Stack part
1
http://ybt.ssoier.cn:8088/problem_show.php?pid=1331
it's a very strange prob.I dont know why i cant pass the test in this website while i can pass in sycoj.so i hold the view that its ybt's fault.there's no bugs in my cpp.

Click to view the code
#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=int(l);i<int(r);i++)
#define per(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define repg(i,l,r) for(i=int(l);i<int(r);i++)
#define perg(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define rept(i,t) for(int i=int(h[t]);~i;i=ne[i])
#define reph(i,t,h) for(int i=int(h[t]);~i;i=ne[i])
#define x first
#define y second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long lll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF=0x3f3f3f3f,mod=1e9+7;
const ll INFF=1e18;
const double eps=1e-14;
mt19937 mrand(random_device{}());
template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
    return out << "(" << p.x << " " << p.y << ")";
}
template<class A> ostream& operator <<(ostream& out, const vector<A> &v) {
    rep(i, 0, sz(v)) {
        if(i) out << " ";
        out << v[i];
    }
    return out;
}
int rnd(int x){return mrand()%x;}
ll qmi(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
ll qmul(ll a,ll b){ll res=0;a%=mod;while(b){if(b&1) res=(res+a)%mod;a=(a+a)%mod;b>>=1;} return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
/*ll read()
{
    ll x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')x=x*10+(c-'0'),c=getchar();
    return f*x;
}*/
//#include <iostream>
//#include <cstdio>
//#include <windows.h>
//#include <cstdlib>
//#include <ctime>
//using namespace std;
//int main()
//{
//    int ok = 0;
//    int n = 50;
//    for (int i = 1; i <= n; ++i)
//    {
//        system("make.exe > make.txt");
//        system("std.exe < make.txt > std.txt");
//        double begin = clock();
//        system("baoli.exe < make.txt > baoli.txt");
//        double end = clock();
//
//        double t = (end - begin);
//        if (system("fc std.txt baoli.txt"))
//        {
//            printf("test point #%d Wrong Answer\n", i);
//        }
//        Else if (T > 1000) / / 1s
//        {
//            printf("test point #%d Time Limited Exceeded time%. 0lfms\n", i, t);
//        }
//        else
//        {
//            printf("test point #%d Accepted time%. 0lfms\n", i, t);
//            ok++; //AC quantity + 1
//        }
//    }
//    printf("\n");
//    double res = 100.0 * ok / n;
//    printf("total% d groups of test data, AC data% d groups. Score%. 1lf.", n, ok, res);
//
//    Sleep(1000); // Sleep for 1 second to save the number of beats.
//}
inline int read()
{
    int x=0;
    char ch;
    bool fx=false;
    do ch=getchar();while(~ch&&ch!='-'&&(ch<48||ch>57));
    if(ch=='-')fx=true,ch=getchar();
    for(;ch>47&&ch<58;ch=getchar())
        x=(x<<1)+(x<<3)+(ch^48);
    return fx?-x:x;
}
string s; 

int main()
{
#ifdef my_test
 	freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
#ifdef my_dp
	while(1)
	{
		system("data.exe > in.txt");
		system("brute.exe < in.txt > brute.txt");
		system("std.exe < in.txt > std.txt");
		if(system("fc std.txt brute.txt"))
			break;
	}
#endif
#ifdef my_dp2
    while (1) //Loop until different data is found
    {
        system("data.exe");
        system("baoli.exe");
        system("std.exe");
        if (system("fc std.txt baoli.txt")) //When fc returns 1, the data is different
            break;                          //Jump out of the loop if it's different
    }
#endif
	clock_t start,end;
    start=clock();
	getline(cin,s);
	int len=s.size();
	stack<ll> q;
	rep(i,0,len)
	{
		if(s[i]>='0'&&s[i]<='9') 
		{
			ll tmp=0;
			while(s[i]>='0'&&s[i]<='9') tmp=tmp*10+s[i]-'0',i++;
			q.push(tmp);	
		}
		else if(s[i]==' ')continue;
		else if(s[i]=='@') break;
		else 
		{
			int a,b;b=q.top(),q.pop(),a=q.top(),q.pop();
			if(s[i]=='+') q.push(a+b);
			if(s[i]=='-') q.push(a-b);
			if(s[i]=='*') q.push(a*b);
			if(s[i]=='/') q.push(a/b);
		}
	}
	printf("%lld\n",q.top());
    end=clock();
//    printf("%lfms\n",(double)(end-start)/CLOCKS_PER_SEC*1000);
    return 0;
}
2

http://ybt.ssoier.cn:8088/problem_show.php?pid=1353

a very easy prob.we have the conception that if the match exists,every right bracket has the corresponding left bracket before. so just count the number of left and right bracket dynamicly.if the cnt>=0 for all the time and it finally turns into zero,we got "yes" otherwise "NO";

3

http://ybt.ssoier.cn:8088/problem_show.php?pid=1354

For convenience, it's still in Chinese. It's outrageous. Local and oj are different. There will be problems. One is that they are not initialized, and the other is that the input data is different from the actual data, such as the space at the end of the line.
Another aspect is that the idea should be clear, otherwise it is easy to miss solutions. The establishment and non establishment should be clearly divided. For example, if the problem is established, it should not only be able to match successfully, but also have no left.

Click to view the code
#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=int(l);i<int(r);i++)
#define per(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define repg(i,l,r) for(i=int(l);i<int(r);i++)
#define perg(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define rept(i,t) for(int i=int(h[t]);~i;i=ne[i])
#define reph(i,t,h) for(int i=int(h[t]);~i;i=ne[i])
#define x first
#define y second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long lll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF=0x3f3f3f3f,mod=1e9+7;
const ll INFF=1e18;
const double eps=1e-14;
mt19937 mrand(random_device{}());
template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
    return out << "(" << p.x << " " << p.y << ")";
}
template<class A> ostream& operator <<(ostream& out, const vector<A> &v) {
    rep(i, 0, sz(v)) {
        if(i) out << " ";
        out << v[i];
    }
    return out;
}
int rnd(int x){return mrand()%x;}
ll qmi(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
ll qmul(ll a,ll b){ll res=0;a%=mod;while(b){if(b&1) res=(res+a)%mod;a=(a+a)%mod;b>>=1;} return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
/*ll read()
{
    ll x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')x=x*10+(c-'0'),c=getchar();
    return f*x;
}*/
//#include <iostream>
//#include <cstdio>
//#include <windows.h>
//#include <cstdlib>
//#include <ctime>
//using namespace std;
//int main()
//{
//    int ok = 0;
//    int n = 50;
//    for (int i = 1; i <= n; ++i)
//    {
//        system("make.exe > make.txt");
//        system("std.exe < make.txt > std.txt");
//        double begin = clock();
//        system("baoli.exe < make.txt > baoli.txt");
//        double end = clock();
//
//        double t = (end - begin);
//        if (system("fc std.txt baoli.txt"))
//        {
//            printf("test point #%d Wrong Answer\n", i);
//        }
//        Else if (T > 1000) / / 1s
//        {
//            printf("test point #%d Time Limited Exceeded time%. 0lfms\n", i, t);
//        }
//        else
//        {
//            printf("test point #%d Accepted time%. 0lfms\n", i, t);
//            ok++; //AC quantity + 1
//        }
//    }
//    printf("\n");
//    double res = 100.0 * ok / n;
//    printf("total% d groups of test data, AC data% d groups. Score%. 1lf.", n, ok, res);
//
//    Sleep(1000); // Sleep for 1 second to save the number of beats.
//}
inline int read()
{
    int x=0;
    char ch;
    bool fx=false;
    do ch=getchar();while(~ch&&ch!='-'&&(ch<48||ch>57));
    if(ch=='-')fx=true,ch=getchar();
    for(;ch>47&&ch<58;ch=getchar())
        x=(x<<1)+(x<<3)+(ch^48);
    return fx?-x:x;
}


int main()
{
#ifdef my_test
 	freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
#ifdef my_dp
	while(1)
	{
		system("data.exe > in.txt");
		system("brute.exe < in.txt > brute.txt");
		system("std.exe < in.txt > std.txt");
		if(system("fc std.txt brute.txt"))
			break;
	}
#endif
#ifdef my_dp2
    while (1) //Loop until different data is found
    {
        system("data.exe");
        system("baoli.exe");
        system("std.exe");
        if (system("fc std.txt baoli.txt")) //When fc returns 1, the data is different
            break;                          //Jump out of the loop if it's different
    }
#endif
	clock_t start,end;
    start=clock();
	string s="";
	getline(cin,s);
	int len=s.size();
	bool flag=true;
	stack<char> q;
	rep(i,0,len-1)//End of line space problem 
	{
		if(s[i]=='(') q.push(s[i]);
		else if(s[i]=='[')q.push(s[i]);
		else if((s[i]==')'||s[i]==']')&&!q.size()) {flag=false;break;} 
		else if(s[i]==')'&&q.top()=='(') {q.pop();} 
		else if(s[i]==']'&&q.top()=='[') {q.pop();}
//		else {flag=false;break;}// You can delete it directly, because if it does not match, it is either (more or) more, (non empty situation will occur), it will directly return false 
	}
	if(flag&&!q.size()) puts("OK");
	else puts("Wrong");
    end=clock();
//    printf("%lfms\n",(double)(end-start)/CLOCKS_PER_SEC*1000);
    return 0;
}


4

http://ybt.ssoier.cn:8088/problem_show.php?pid=1355
The space at the end of the line is always painful. getline can't be used casually. It will read everything. It's best to read it according to cin, unless it is intermittent. In addition, the judgment of size is compared by numbers. In addition, for the situation, if the good situation is easy to list, it is good, and if the bad situation is easy to list, it is bad.

Click to view the code
#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=int(l);i<int(r);i++)
#define per(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define repg(i,l,r) for(i=int(l);i<int(r);i++)
#define perg(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define rept(i,t) for(int i=int(h[t]);~i;i=ne[i])
#define reph(i,t,h) for(int i=int(h[t]);~i;i=ne[i])
#define x first
#define y second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long lll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF=0x3f3f3f3f,mod=1e9+7;
const ll INFF=1e18;
const double eps=1e-14;
mt19937 mrand(random_device{}());
template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
    return out << "(" << p.x << " " << p.y << ")";
}
template<class A> ostream& operator <<(ostream& out, const vector<A> &v) {
    rep(i, 0, sz(v)) {
        if(i) out << " ";
        out << v[i];
    }
    return out;
}
int rnd(int x){return mrand()%x;}
ll qmi(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
ll qmul(ll a,ll b){ll res=0;a%=mod;while(b){if(b&1) res=(res+a)%mod;a=(a+a)%mod;b>>=1;} return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
/*ll read()
{
    ll x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')x=x*10+(c-'0'),c=getchar();
    return f*x;
}*/
//#include <iostream>
//#include <cstdio>
//#include <windows.h>
//#include <cstdlib>
//#include <ctime>
//using namespace std;
//int main()
//{
//    int ok = 0;
//    int n = 50;
//    for (int i = 1; i <= n; ++i)
//    {
//        system("make.exe > make.txt");
//        system("std.exe < make.txt > std.txt");
//        double begin = clock();
//        system("baoli.exe < make.txt > baoli.txt");
//        double end = clock();
//
//        double t = (end - begin);
//        if (system("fc std.txt baoli.txt"))
//        {
//            printf("test point #%d Wrong Answer\n", i);
//        }
//        Else if (T > 1000) / / 1s
//        {
//            printf("test point #%d Time Limited Exceeded time%. 0lfms\n", i, t);
//        }
//        else
//        {
//            printf("test point #%d Accepted time%. 0lfms\n", i, t);
//            ok++; //AC quantity + 1
//        }
//    }
//    printf("\n");
//    double res = 100.0 * ok / n;
//    printf("total% d groups of test data, AC data% d groups. Score%. 1lf.", n, ok, res);
//
//    Sleep(1000); // Sleep for 1 second to save the number of beats.
//}
inline int read()
{
    int x=0;
    char ch;
    bool fx=false;
    do ch=getchar();while(~ch&&ch!='-'&&(ch<48||ch>57));
    if(ch=='-')fx=true,ch=getchar();
    for(;ch>47&&ch<58;ch=getchar())
        x=(x<<1)+(x<<3)+(ch^48);
    return fx?-x:x;
}
char a[]={'{','[','(','<','}',']',')','>'};
map<char,int> p;
int main()
{
#ifdef my_test
 	freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
#ifdef my_dp
	while(1)
	{
		system("data.exe > in.txt");
		system("brute.exe < in.txt > brute.txt");
		system("std.exe < in.txt > std.txt");
		if(system("fc std.txt brute.txt"))
			break;
	}
#endif
#ifdef my_dp2
    while (1) //Loop until different data is found
    {
        system("data.exe");
        system("baoli.exe");
        system("std.exe");
        if (system("fc std.txt baoli.txt")) //When fc returns 1, the data is different
            break;                          //Jump out of the loop if it's different
    }
#endif
	clock_t start,end;
    start=clock();
	int n;n=read();
	rep(i,0,8) p[a[i]]=i; 
	while(n--)
	{
		string s;
		cin>>s;
		int len=s.size();
		stack<int> q;
		bool flag=true;
		rep(i,0,len)
		{
			if(s[i]=='('||s[i]=='{'||s[i]=='['||s[i]=='<') //getline is risky and should be used with caution. 
			{
				if(!q.size()) q.push(p[s[i]]);
 				else 
 				{
 					if(q.top()>p[s[i]]) {flag=false;break;}
					q.push(p[s[i]]);
				}
			}
			else if(!q.size()){flag=false;break;}//To prevent problems, you can't top without 
			else if(s[i]==')'&&q.top()==2||(s[i]==']'&&q.top()==1)||(s[i]=='>'&&q.top()==3)||(s[i]=='}'&&q.top()==0)) {q.pop();}
//			else q.pop();// Match successfully 
		}
		if(flag&&!q.size()) puts("YES");//The match is successful, and there is no one left 
		else puts("NO"); 
	}
    end=clock();
//    printf("%lfms\n",(double)(end-start)/CLOCKS_PER_SEC*1000);
    return 0;
}


5

http://ybt.ssoier.cn:8088/problem_show.php?pid=1357

When the train enters and leaves the station, the precondition is first. If he wants to leave the stack, the one in front of him must either leave the stack or still be in the stack, and the exit itself is realized by its exit, and only simulation is needed in the stack.
In addition, first implement the preconditions, and then see whether they can be out of the stack under the current situation. If there is a larger number in the stack, it will not be out of the stack, so it cannot be realized.

Click to view the code
#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=int(l);i<int(r);i++)
#define per(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define repg(i,l,r) for(i=int(l);i<int(r);i++)
#define perg(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define rept(i,t) for(int i=int(h[t]);~i;i=ne[i])
#define reph(i,t,h) for(int i=int(h[t]);~i;i=ne[i])
#define x first
#define y second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long lll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF=0x3f3f3f3f,mod=1e9+7;
const ll INFF=1e18;
const double eps=1e-14;
mt19937 mrand(random_device{}());
template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
    return out << "(" << p.x << " " << p.y << ")";
}
template<class A> ostream& operator <<(ostream& out, const vector<A> &v) {
    rep(i, 0, sz(v)) {
        if(i) out << " ";
        out << v[i];
    }
    return out;
}
int rnd(int x){return mrand()%x;}
ll qmi(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
ll qmul(ll a,ll b){ll res=0;a%=mod;while(b){if(b&1) res=(res+a)%mod;a=(a+a)%mod;b>>=1;} return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
/*ll read()
{
    ll x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')x=x*10+(c-'0'),c=getchar();
    return f*x;
}*/
//#include <iostream>
//#include <cstdio>
//#include <windows.h>
//#include <cstdlib>
//#include <ctime>
//using namespace std;
//int main()
//{
//    int ok = 0;
//    int n = 50;
//    for (int i = 1; i <= n; ++i)
//    {
//        system("make.exe > make.txt");
//        system("std.exe < make.txt > std.txt");
//        double begin = clock();
//        system("baoli.exe < make.txt > baoli.txt");
//        double end = clock();
//
//        double t = (end - begin);
//        if (system("fc std.txt baoli.txt"))
//        {
//            printf("test point #%d Wrong Answer\n", i);
//        }
//        Else if (T > 1000) / / 1s
//        {
//            printf("test point #%d Time Limited Exceeded time%. 0lfms\n", i, t);
//        }
//        else
//        {
//            printf("test point #%d Accepted time%. 0lfms\n", i, t);
//            ok++; //AC quantity + 1
//        }
//    }
//    printf("\n");
//    double res = 100.0 * ok / n;
//    printf("total% d groups of test data, AC data% d groups. Score%. 1lf.", n, ok, res);
//
//    Sleep(1000); // Sleep for 1 second to save the number of beats.
//}
inline int read()
{
    int x=0;
    char ch;
    bool fx=false;
    do ch=getchar();while(~ch&&ch!='-'&&(ch<48||ch>57));
    if(ch=='-')fx=true,ch=getchar();
    for(;ch>47&&ch<58;ch=getchar())
        x=(x<<1)+(x<<3)+(ch^48);
    return fx?-x:x;
}


int main()
{
#ifdef my_test
 	freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
#ifdef my_dp
	while(1)
	{
		system("data.exe > in.txt");
		system("brute.exe < in.txt > brute.txt");
		system("std.exe < in.txt > std.txt");
		if(system("fc std.txt brute.txt"))
			break;
	}
#endif
#ifdef my_dp2
    while (1) //Loop until different data is found
    {
        system("data.exe");
        system("baoli.exe");
        system("std.exe");
        if (system("fc std.txt baoli.txt")) //When fc returns 1, the data is different
            break;                          //Jump out of the loop if it's different
    }
#endif
	clock_t start,end;
    start=clock();
    int n; 
	n=read();
	int cur=1;
	stack<int> q;
	rep(i,1,n+1)//If you want to stack, first of all, the numbers smaller than him must be either on the stack or have been out of the stack, and have been out of the stack, which can be realized by the stack out process itself 
	{//This is the condition, and then simulate in and out of the stack under this condition, and then see if it can be realized 
		int x;x=read();
		while(cur<=x) q.push(cur++);
		if(q.top()==x) q.pop();//In the simulation process, if the number larger than him is still in the stack, it is impossible to get out of the stack. 
		else {puts("NO");return 0;}//If you can't get out, then fail 
	} 
	puts("YES");
    end=clock();
//    printf("%lfms\n",(double)(end-start)/CLOCKS_PER_SEC*1000);
    return 0;
}


6

http://ybt.ssoier.cn:8088/problem_show.php?pid=1356

Although it is a very annoying topic, I personally think it is a good topic to test your realization of the problem.
The first is how to simulate infix expressions.
1 prioritize 2 prevent unexpected situations
Based on this, first read in the number, and then encounter the special treatment of parentheses. Then, if it is the beginning, it does not count because there is no symbol. If it is the parenthesis, it does not count because the priority is not high. The normal situation is number + symbol + number + symbol. As long as the symbol at this time is greater than the previous symbol, it can be calculated

Click to view the code
#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=int(l);i<int(r);i++)
#define per(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define repg(i,l,r) for(i=int(l);i<int(r);i++)
#define perg(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define rept(i,t) for(int i=int(h[t]);~i;i=ne[i])
#define reph(i,t,h) for(int i=int(h[t]);~i;i=ne[i])
#define x first
#define y second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long lll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF=0x3f3f3f3f,mod=1e9+7;
const ll INFF=1e18;
const double eps=1e-14;
mt19937 mrand(random_device{}());
template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
    return out << "(" << p.x << " " << p.y << ")";
}
template<class A> ostream& operator <<(ostream& out, const vector<A> &v) {
    rep(i, 0, sz(v)) {
        if(i) out << " ";
        out << v[i];
    }
    return out;
}
int rnd(int x){return mrand()%x;}
ll qmi(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
ll qmul(ll a,ll b){ll res=0;a%=mod;while(b){if(b&1) res=(res+a)%mod;a=(a+a)%mod;b>>=1;} return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
/*ll read()
{
    ll x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')x=x*10+(c-'0'),c=getchar();
    return f*x;
}*/
//#include <iostream>
//#include <cstdio>
//#include <windows.h>
//#include <cstdlib>
//#include <ctime>
//using namespace std;
//int main()
//{
//    int ok = 0;
//    int n = 50;
//    for (int i = 1; i <= n; ++i)
//    {
//        system("make.exe > make.txt");
//        system("std.exe < make.txt > std.txt");
//        double begin = clock();
//        system("baoli.exe < make.txt > baoli.txt");
//        double end = clock();
//
//        double t = (end - begin);
//        if (system("fc std.txt baoli.txt"))
//        {
//            printf("test point #%d Wrong Answer\n", i);
//        }
//        Else if (T > 1000) / / 1s
//        {
//            printf("test point #%d Time Limited Exceeded time%. 0lfms\n", i, t);
//        }
//        else
//        {
//            printf("test point #%d Accepted time%. 0lfms\n", i, t);
//            ok++; //AC quantity + 1
//        }
//    }
//    printf("\n");
//    double res = 100.0 * ok / n;
//    printf("total% d groups of test data, AC data% d groups. Score%. 1lf.", n, ok, res);
//
//    Sleep(1000); // Sleep for 1 second to save the number of beats.
//}
inline int read()
{
    int x=0;
    char ch;
    bool fx=false;
    do ch=getchar();while(~ch&&ch!='-'&&(ch<48||ch>57));
    if(ch=='-')fx=true,ch=getchar();
    for(;ch>47&&ch<58;ch=getchar())
        x=(x<<1)+(x<<3)+(ch^48);
    return fx?-x:x;
}
const int N=1e6;
int n,m;
char st[N];
stack<int> s1;
stack<char> s2;
int level(char x)
{
	if(x=='+'||x=='-') return 1;
	else if(x=='*'||x=='/') return 2;
	else if(x=='^') return 3;
	return 0;
} 
void cal(stack<int> &s1,stack<char> &s2)
{
	int y=s1.top();s1.pop();
	int x=s1.top();s1.pop();
	char z=s2.top();s2.pop();
	if(z=='+')s1.push(x+y);
	if(z=='-') s1.push(x-y);
	if(z=='*') s1.push(x*y);
	if(z=='/' )s1.push(x/y);
	if(z=='^') s1.push(int(pow(x,y)));
}
int main()
{
#ifdef my_test
 	freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
#ifdef my_dp
	while(1)
	{
		system("data.exe > in.txt");
		system("brute.exe < in.txt > brute.txt");
		system("std.exe < in.txt > std.txt");
		if(system("fc std.txt brute.txt"))
			break;
	}
#endif
#ifdef my_dp2
    while (1) //Loop until different data is found
    {
        system("data.exe");
        system("baoli.exe");
        system("std.exe");
        if (system("fc std.txt baoli.txt")) //When fc returns 1, the data is different
            break;                          //Jump out of the loop if it's different
    }
#endif
	clock_t start,end;
    start=clock();
	scanf("%s",st+1);
	int n=strlen(st+1);
	int cnt=0;
	bool flag=true;
	rep(i,1,n+1)
	{	
		if(st[i]=='(') cnt++;
		else if(st[i]==')') 
		{
			cnt--;
			if(cnt<0) {flag=false;break;}
		}
	}
	rep(i,2,n+1) if(level(st[i])&&level(st[i-1])) {flag=false;break;}	
	if(n==1&&level(st[1])||!flag){puts("NO");return 0;}
	int tmp=0;
	flag=false;
	rep(i,1,n+1) 
	{
		if(st[i]>='0'&&st[i]<='9')
		{
			tmp=(tmp<<3)+(tmp<<1)+st[i]-'0';
			flag=true;
		}
		else 
		{
			if(flag) s1.push(tmp),tmp=0,flag=false;//If the previous one is a number, save it. 
			if(st[i]=='('){s2.push(st[i]);continue;}//If it's not a number, it's parentheses, so handle parentheses 
			if(st[i]==')') 
			{
				while(s2.top()!='(') cal(s1,s2);
				s2.pop();continue;
			}
			while(!s2.empty()&&level(s2.top())>=level(st[i]))//If the previous is not a bracket and can be operated on 
			{
				cal(s1,s2);	
			}//Not parentheses, not numbers, only symbols 
			s2.push(st[i]);	//Save symbol 
		}
	}//Only when the priority of the top element of the stack is greater than or equal to the priority of my current symbol can I calculate the current two elements
	//At the same time, symbols have the highest priority, so they should be processed optimally
	//Infix expression is 1 + 1 before calculation
	//0 is used in parentheses to prevent only one number. Start counting directly in parentheses. / / there is no symbol at the beginning, and parentheses have little priority 
	if(flag) s1.push(tmp),tmp=0,flag=false;
	while(!s2.empty())cal(s1,s2);
	printf("%d\n",s1.top());
    end=clock();
//    printf("%lfms\n",(double)(end-start)/CLOCKS_PER_SEC*1000);
    return 0;
}


7

http://ybt.ssoier.cn:8088/problem_show.php?pid=1358
Like the previous question, it is an infix expression
It's actually a simulation idea

Click to view the code
#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=int(l);i<int(r);i++)
#define per(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define repg(i,l,r) for(i=int(l);i<int(r);i++)
#define perg(i,r,l) for(int i=int(r-1);i>=int(l);i--)
#define rept(i,t) for(int i=int(h[t]);~i;i=ne[i])
#define reph(i,t,h) for(int i=int(h[t]);~i;i=ne[i])
#define x first
#define y second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long lll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF=0x3f3f3f3f,mod=1e9+7;
const ll INFF=1e18;
const double eps=1e-14;
mt19937 mrand(random_device{}());
template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
    return out << "(" << p.x << " " << p.y << ")";
}
template<class A> ostream& operator <<(ostream& out, const vector<A> &v) {
    rep(i, 0, sz(v)) {
        if(i) out << " ";
        out << v[i];
    }
    return out;
}
int rnd(int x){return mrand()%x;}
ll qmi(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
ll qmul(ll a,ll b){ll res=0;a%=mod;while(b){if(b&1) res=(res+a)%mod;a=(a+a)%mod;b>>=1;} return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
/*ll read()
{
    ll x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')x=x*10+(c-'0'),c=getchar();
    return f*x;
}*/
//#include <iostream>
//#include <cstdio>
//#include <windows.h>
//#include <cstdlib>
//#include <ctime>
//using namespace std;
//int main()
//{
//    int ok = 0;
//    int n = 50;
//    for (int i = 1; i <= n; ++i)
//    {
//        system("make.exe > make.txt");
//        system("std.exe < make.txt > std.txt");
//        double begin = clock();
//        system("baoli.exe < make.txt > baoli.txt");
//        double end = clock();
//
//        double t = (end - begin);
//        if (system("fc std.txt baoli.txt"))
//        {
//            printf("test point #%d Wrong Answer\n", i);
//        }
//        Else if (T > 1000) / / 1s
//        {
//            printf("test point #%d Time Limited Exceeded time%. 0lfms\n", i, t);
//        }
//        else
//        {
//            printf("test point #%d Accepted time%. 0lfms\n", i, t);
//            ok++; //AC quantity + 1
//        }
//    }
//    printf("\n");
//    double res = 100.0 * ok / n;
//    printf("total% d groups of test data, AC data% d groups. Score%. 1lf.", n, ok, res);
//
//    Sleep(1000); // Sleep for 1 second to save the number of beats.
//}
inline int read()
{
    int x=0;
    char ch;
    bool fx=false;
    do ch=getchar();while(~ch&&ch!='-'&&(ch<48||ch>57));
    if(ch=='-')fx=true,ch=getchar();
    for(;ch>47&&ch<58;ch=getchar())
        x=(x<<1)+(x<<3)+(ch^48);
    return fx?-x:x;
}
const int N=1e6;
int n,m;
char st[N];
stack<int> s1;
stack<char> s2;
int level(char x)
{
	if(x=='+'||x=='-') return 1;
	else if(x=='*'||x=='/') return 2;
	else if(x=='^') return 3;
	return 0;
} 
void cal(stack<int> &s1,stack<char> &s2)
{
	int y=s1.top();s1.pop();
	int x=s1.top();s1.pop();
	char z=s2.top();s2.pop();
	if(z=='+')s1.push(x+y);
	if(z=='-') s1.push(x-y);
	if(z=='*') s1.push(x*y);
	if(z=='/' )s1.push(x/y);
	if(z=='^') s1.push(int(pow(x,y)));
}
int main()
{
#ifdef my_test
 	freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
#ifdef my_dp
	while(1)
	{
		system("data.exe > in.txt");
		system("brute.exe < in.txt > brute.txt");
		system("std.exe < in.txt > std.txt");
		if(system("fc std.txt brute.txt"))
			break;
	}
#endif
#ifdef my_dp2
    while (1) //Loop until different data is found
    {
        system("data.exe");
        system("baoli.exe");
        system("std.exe");
        if (system("fc std.txt baoli.txt")) //When fc returns 1, the data is different
            break;                          //Jump out of the loop if it's different
    }
#endif
	clock_t start,end;
    start=clock();
	scanf("%s",st+1);
	int n=strlen(st+1);
	int cnt=0;
	bool flag=true;
	rep(i,1,n)
	{	
		if(st[i]=='(') cnt++;
		else if(st[i]==')') 
		{
			cnt--;
			if(cnt<0) {flag=false;break;}
		}
	}
	rep(i,2,n) if(level(st[i])&&level(st[i-1])) {flag=false;break;}	
	if(n==1&&level(st[1])||!flag){puts("NO");return 0;}
	int tmp=0;
	flag=false;
	rep(i,1,n) 
	{
		if(st[i]>='0'&&st[i]<='9')
		{
			tmp=(tmp<<3)+(tmp<<1)+st[i]-'0';
			flag=true;
		}
		else 
		{
			if(flag) s1.push(tmp),tmp=0,flag=false;//If the previous one is a number, save it. 
			if(st[i]=='('){s2.push(st[i]);continue;}//If it's not a number, it's parentheses, so handle parentheses 
			if(st[i]==')') 
			{
				while(s2.top()!='(') cal(s1,s2);
				s2.pop();continue;
			}
			while(!s2.empty()&&level(s2.top())>=level(st[i]))//If the previous is not a bracket and can be operated on 
			{
				cal(s1,s2);	
			}//Not parentheses, not numbers, only symbols 
			s2.push(st[i]);	//Save symbol 
		}
	}//Only when the priority of the top element of the stack is greater than or equal to the priority of my current symbol can I calculate the current two elements
	//At the same time, symbols have the highest priority, so they should be processed optimally
	//Infix expression is 1 + 1 before calculation
	//0 is used in parentheses to prevent only one number. Start counting directly in parentheses. / / there is no symbol at the beginning, and parentheses have little priority 
	if(flag) s1.push(tmp),tmp=0,flag=false;
	while(!s2.empty())cal(s1,s2);
	printf("%d\n",s1.top());
    end=clock();
//    printf("%lfms\n",(double)(end-start)/CLOCKS_PER_SEC*1000);
    return 0;
}


Topics: data structure stack