判断表达式是否合法 以及是否正确;
合法 数学合法即可;
正确性 : 整个式子为 a 操作 b 型 a,b为整体 或 是被括号 形成的整体;
a+a proper;
(a)+((a+b)) improper;
a+(a+b)*c improper;
a+((a+b)*c) properl

题目描述

The correspondence among the operators and the operands can be clarified using parentheses. In a C program, for example, the expression a-b-c can be clarified as (a-b)-c since the minus operator is left associative.
The parentheses can be used to override the default precedences and associativities of operators. For instance, in the expression a-(b-c), the left associativity of the minus operator is overridden by the parentheses.
A novice C programmer Dennis has been stressed too much in remembering the operator precedences and associativities. Therefore, he made a new language namely ICPC (I can parenthesize C), in which the operator-operand correspondence should be clarified fully using parentheses; except for this, all the other features are same as C. For instance, one should write (a-b)-c instead of a-b-c and a+(bc) rather than a+bc.
However, the usage of the parentheses can be too much in some cases. For the expression a-b-c, it is enough to write
(a-b)-c
but one can write it as
(a-(b))-c
or as
((a-b)-c)
where the pairs of parentheses underlined are superfluous.
Dennis wants to convert the C expression into the ICPC expression, in which the pairs of parentheses should be used exactly as needed. You have to help Dennis. For simplicity, you can assume that the input C expression contains only five binary arithmetic operators (+, -, *, /, and %), left and right parentheses ( and ), and single-lowercase alphabet operands. Given such a C expression, write a program to determine whether it is an ICPC expression or not.
If the expression is not an error in ICPC, then it should not be an error in C. Once it is not an error in C, the usage of parentheses should be checked to determine whether it is a proper expression in ICPC or not. If the expression is not properly parenthesized, i.e., the number of parentheses is not exact as needed, then it is considered improper.
Beware that some of the input C expressions may be erroneous originally. For instance, a%/b is an error since it requires one more operand between % and / to be valid. As another example, a b + c is also an error since it requires one more operator between a and b.

输入

Your program is to read from standard input. The input consists of a single line containing a C expression. The expression is a string of single-lowercase alphabets, special symbols including left and right parentheses and five binary arithmetic operators (+, -, *, /, and %), and spaces. The input line contains at least one operand. The length of the input line (the number of characters in it) is no more than 1000 including the spaces and the single newline character at the end.

输出

Your program is to write to standard output. Print exactly one line. The line should contain one of the following words: error, proper, and improper. Print error if the input C expression is erroneous. Once it is not an error, print proper or improper depending on the parenthesized status of the expression: print proper if it is parenthesized properly with the exact number of parentheses needed for ICPC, and print improper otherwise.

样例输入
复制样例数据 a + a

样例输出
proper

代码如下 包含两种 work 的函数
一种使用栈 一种没用
好像不用栈会快一些;

#include<bits/stdc++.h>
using namespace std;
char a[112345];
int id[112345];
int n;
int ok()
{n=strlen(a);for(int i=0;i<n;i++){if(isalpha(a[i]))id[i]=0;if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'||a[i]=='%')id[i]=1;if(a[i]=='(')id[i]=2;if(a[i]==')')id[i]=3;}if(id[0]==3||id[0]==1||id[n-1]==2||id[n-1]==1)return 0;if(n==1&&id[0]==0)return 1;int x,y;for(int i=1;i<n;++i){x=id[i-1];y=id[i];if(x==0){if(y==2||y==0)return 0;}if(x==1){if(y==1||y==3)return 0;}if(x==2){if(y==1||y==3)return 0;}if(x==3){if(y==0||y==2)return 0;}}int cnt=0;for(int i=0;i<n;i++){if(id[i]==2)cnt++;else{if(id[i]==3){if(cnt==0)return 0;cnt--;}}}if(cnt==0)return 1;elsereturn 0;
}
int work()
{int zhan[112345];memset(zhan,-1,sizeof(zhan));if(n==1)return 1;int cnt=0,ans=0;for(int i=0;i<n;i++)if(id[i]==1)ans++;int j=0;for(int i=0;i<n;i++){if(id[i]==0)continue;if(id[i]<=2){zhan[++j]=id[i];}if(id[i]==3){int k;int flag=0;for(k=j;k>=1;k--){if(zhan[k]==1)flag++;if(zhan[k]==2)break;}j=k-1;if(flag==1){cnt++;}elsereturn 0;}}if(ans==cnt+1)return 1;elsereturn 0;
}int work()
{int zhan[112345];memset(zhan,-1,sizeof(zhan));if(n==1)return 1;int cnt=0,ans=0;for(int i=0;i<n;i++)if(id[i]==1)ans++;stack<int>sta;int j=0;for(int i=0;i<n;i++){if(id[i]==0)continue;if(id[i]<=2){sta.push(id[i]);}if(id[i]==3){int k;int flag=0;while(!sta.empty()){int Top=sta.top();sta.pop();if(Top==2)break;elseflag++;}if(flag==1){cnt++;}elsereturn 0;}}if(ans==cnt+1)return 1;elsereturn 0;
}
int main()
{gets(a);int l=strlen(a);int j=0;for(int i=0;i<l;i++){if(a[i]!=' '){a[j++]=a[i];}}a[j]=0;if( !ok() )printf("error\n");else{if( work() )printf("proper\n");elseprintf("improper\n");}return 0;
}

备战省赛4 问题 F: Parentheses相关推荐

  1. 【备战美赛】重要!2023年美赛官方发布最新通知

    备战美赛 春节假期结束,各项比赛也需要准备起来啦!近日,美赛组委会发布了2023年官方最新邮件,邮件内容主要是介绍本届竞赛基本情况.参赛规则.竞赛奖励.资源下载等相关内容,确定了比赛时间为北京时间2月 ...

  2. 数学建模美赛E、F题备考策略(自用,大部分复制粘贴)

    这里要讲一下故事的背景,我们小组三个人都是大一大二的学生,我的队友们都是数学专业的学生,所以比赛中的编程部分就交给了我这样的工业工程系的选手.我们在看完了历年赛题后一直认为:前面的几题我们都很难建立出 ...

  3. 浙江工业大学大学生程序设计迎新赛预赛( F,小妈妈找蝌蚪 L ,取数游戏 M,小杰的签到题)

    F,妈妈找小蝌蚪 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format: %lld 题目描述 青蛙妈妈最近很不放心把蝌 ...

  4. 备战省赛组队训练赛第一场

    目录 问题 A: 篮球队选拔 问题 B: 黑暗意志 问题 C: 调酒壶里的酸奶 问题 D: 过分的谜题 问题 E: 不存在的泳池 问题 F: fps游戏 问题 G: 流连人间的苏苏 问题 H: 路哥从 ...

  5. latex 下划线_备战美赛!论文写作必备Latex排版教程之单词间隔、标题及交叉引用...

    LaTex排版学习往期回顾: Latex源文件及文档布局教程 Latex排版之断行.分页与字符串教程 今日学习 单词的间隔 为了使输出的右边界对齐,LATEX 在单词间插入不等的间隔.在句子的末尾插入 ...

  6. 备战美赛,这些你应该知道的知识点

    国赛刚过完 美赛就接踵而来 只剩下两个多月的时间 不少准备参赛的小伙伴在后台咨询 该如何准备美赛 为了更好地帮助大家进攻美赛 超模君特意邀请到 今年国赛A题Matlab创新奖&一等奖获奖者 方 ...

  7. 电赛2019年F题纸张测量FDC2214的初始化代码(含STM32f103zet6和f103c8t6)胎教式

    (一)我的感受 其实第一次接触这一个题目的时候,觉得还是不难的,(因为相对于我实验室的同学做的其他的一些控制类题目).我觉得在代码方面完成题目要求的不难,就是在机械结构方面要做好,我做的装置机械机构不 ...

  8. 埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛(A E F )

    题目链接 :https://www.nowcoder.com/acm/contest/91#question A 移动的可以看成从自己旁边移过来的 A B C A->C 和 A-->B - ...

  9. 【备战美赛】Lingo与规划问题

    引入 规划问题其实我们在高一数学必修五里面已经见过,当时的题目都是线性规划模型,做法也非常直观:通过画图和观察加上一点点计算得出最优解.当时我们处理的是只有x和y两个变量并且约束条件都是一次函数的情况 ...

最新文章

  1. C++多态中虚函数的深入理解
  2. 升级 Visual Studio 2015 CTP 5 的坑、坑、坑
  3. python收取wss数据_Python金融应用之提取交易日+合并截面数据
  4. python对象点方法_python对象方法、类方法、静态方法
  5. Wannafly挑战赛22 B 字符路径 ( 拓扑排序+dp )
  6. 多线程的单元测试工具 - GroboUtils
  7. 【script】python 中文汉字与url的转换
  8. leetcode题解8-盛最多水的容器
  9. Java虚拟机学习总结(2)——性能优化的一般性原则、层次与通用方法
  10. 在win32/安卓开发环境下编译BOX2D代码
  11. javascript 操作cookie
  12. C++静态库和动态库用法
  13. 51单片机c语言烧录软件,51单片机烧写程序的方法
  14. Eclipse开发环境配置
  15. Java小白学习指南【day43】---Linux
  16. C语言题目:输出三角形面积和周长 (15 分)
  17. pandox怎么用_神器Pandoc的安装与使用 | Flyaway's Blog
  18. 菜鸟谈VBA最最基础入门
  19. MySQL 数据类型和约束(外键是重点
  20. HOOF(Histogram of Oriented Optical Flow)特征

热门文章

  1. Aviator 规则引擎介绍
  2. 深度学习优化算法之动量法[公式推导](MXNet)
  3. 车辆在刹车不及时导致与行人发生碰撞事故,产生出险记录
  4. Codeforces H. Ancient Wisdom
  5. 2022-2028全球与中国溴化锂市场现状及未来发展趋势
  6. java 标识符无效_查询y子查询中的标识符无效
  7. 解决wiki图片无法显示问题
  8. 经典按键java手机游戏跑酷,快来收藏!
  9. con和com开头单词规律_背单词其实有诀窍!孩子掌握这15条自然拼读规则,效果事半功倍!...
  10. 了解的GNU/Linux起源