描述

给出一个表达式,其中运算符仅包含+,-,*,/,^(加 减 乘 整除 乘方)要求求出表达式的最终值

数据可能会出现括号情况,还有可能出现多余括号情况

数据保证不会出现>=2^31的答案

数据可能会出现负数情况

输入格式

仅一行,即为表达式

输出格式

仅一行,既为表达式算出的结果

样例输入

(2+2)^(1+1)

样例输出

16

数据范围与约定

  • 表达式总长度<=30

题解:遇到左括号进栈;
遇到右括号与栈顶元素配对,成功则+=2,失败则清空栈顶,重置计数。另外,若栈为空,需重置计数之后continue;

//#include<bits/stdc++.h>
#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include  <cassert>
#include   <cstdio>
#include   <vector>
#include   <string>
#include    <cmath>
#include    <queue>
#include    <stack>
#include      <set>
#include      <map>
using namespace std;
#define P(a,b,c) make_pair(a,make_pair(b,c))
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef pair<int,pair<int,int> >pii;
typedef long long ll;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
ll gcd(ll a, ll b) {
    return b ? gcd(b, a%b) : a;
}
using namespace std;
const int maxn=1e6+10;
template <class T>
inline bool scan_d(T &ret) {
    char c;
    int sgn;
    if(c=getchar(),c==EOF) return 0; //EOF
    while(c!='-'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    ret*=sgn;
    return 1;
}
inline void outt(int x) {
    if(x>9) outt(x/10);
    putchar(x%10+'0');
}
bool is_left(char ch){
	if(ch=='{'||ch=='['||ch=='(')return true;
	return false;
}
char s[maxn];
stack<char>SS;
bool ojbk(char c1,char c2){
	if(abs(c2-c1)<=2){
		return true;
	}
	return false;
}
int main(){
	scanf("%s", s+1);
	int len=strlen(s+1);
	int maxx=0,cnt=0;
	while(!SS.empty())SS.pop();
	rep(i,1,len){
		if(is_left(s[i])){
			SS.push(s[i]);
		}else{
			if(SS.empty()){
				cnt=0;
				continue;
			}
			char t=SS.top();
			if(ojbk(t,s[i])){
				cnt+=2;
				SS.pop();
			}else{
				cnt=0;
				while(!SS.empty()){
					SS.pop();
				}
			}
			maxx=max(maxx,cnt);
		}
	}
	cout<<maxx<<endl;
	return 0;
} 

发表评论

邮箱地址不会被公开。 必填项已用*标注