题目链接:
http://poj.org/problem?id=2372
View Code
题意:给你一串字符,让你判断各种请况,读题要仔细。
分析:判断()完整,()内只能有"=+-*/0123456789"和'\n',(* 和*)也要匹配。中间什么都可以,(*开始,遇到*)结束,表示注释。
若满足上述条件,输出YES,反之输出NO。
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
#include<cstdio> #include<cmath> #include<cstring> #include<queue> #include<stack> #include<cstdlib> #include<iomanip> #include<string> #include<vector> #include<map> #include<string> #include<iostream> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f typedef long long ll; #define Max(a,b) (a>b)?a:b #define lowbit(x) x&(-x) int main() { map<char,bool>vis; vis['1']=1; vis['2']=1; vis['3']=1; vis['4']=1; vis['5']=1; vis['6']=1; vis['7']=1; vis['8']=1; vis['9']=1; vis['0']=1; vis['+']=1; vis['-']=1; vis['*']=1; vis['/']=1; vis['\n']=1; vis['=']=1; char t,last=0; int sum=0,flag=0,ans=1; while(~scanf("%c",&t)) { if(flag==1||ans==0) { if(t==')') { if(last=='*') { flag=0; } } last=t; continue; } if(t=='*'&&last=='(') { flag=1,sum--; continue; } else if(sum==0&&t==')'&&last=='*') ans=0; if(sum<0) { ans=0; continue; } else if(sum>0) { if(t==')') { sum--; } else if(t=='(') sum++; else if(vis[t]==0) ans=0; last=t; } else { if(t=='(') { sum++; } else if(t==')') sum--; last=t; } } if(sum||flag||ans==0) puts("NO"); else puts("YES"); }