博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NOIP2017 D1T2 时间复杂度
阅读量:4323 次
发布时间:2019-06-06

本文共 1992 字,大约阅读时间需要 6 分钟。

NOIP2017 D1T2 时间复杂度

本题用栈模拟,我感觉会写中缀表达式求值那种难度题的人就可以在NOIP上A了此题了。(虽然我挂了,凉了,我也要用嘶哑的声音呼喊出:这题真水)

用栈pop和push的时候 维护 有效循环层数、无效循环(l>r)层数、相同字母  ,每次退栈时更新答案,就OK了 。

还有就是字符串的读入和处理不能写挂。

(在判定ERR之后把剩下的东西读干净再走)

 

#include
#define INF 1000000007using namespace std;int T,head,N,Map[200],cnt1,cnt2,res,ans;//cnt1有效循环 cnt2无效循环 char s[50];struct Node{ char c;int a,b;}Stack[200];int getint(){ int sum=0,i=0,len=strlen(s); while(s[i]<'0'||s[i]>'9')i++; while(s[i]>='0'&&s[i]<='9'&&i<=len){sum=sum*10+s[i]-'0';i++;} return sum;}void Pop(){ Map[Stack[head].c-'a'+1]=0; if(Stack[head].a>Stack[head].b)cnt2--; if(Stack[head].b==INF&&Stack[head].a!=INF&&cnt2==0)cnt1--; head--;}void Push(Node x){ Map[x.c-'a'+1]=1; if(x.a>x.b)cnt2++; else if(x.b==INF&&x.a!=INF&&cnt2==0)cnt1++,res=max(res,cnt1); Stack[++head]=x;}void ToError(int x){ for(int i=x+1;i<=N;i++){ scanf("%s",s); if(s[0]=='F'){scanf("%s",s);scanf("%s",s);scanf("%s",s);} }}int main(){ scanf("%d",&T); while(T--){ while(head>0)Pop(); res=0; scanf("%d%s",&N,s); if(s[2]=='n'){ans=getint();if(!ans)ans=1;} else ans=0; for(int i=1;i<=N;i++){ scanf("%s",s); if(s[0]=='F'){ Node x; scanf("%s",s);x.c=s[0]; scanf("%s",s); if(s[0]=='n')x.a=INF;else x.a=getint(); scanf("%s",s); if(s[0]=='n')x.b=INF;else x.b=getint(); if(Map[x.c-'a'+1]){ ToError(i); puts("ERR");goto New; } Push(x); } else{ if(head==0){ ToError(i); puts("ERR");goto New; } Pop(); } } if(head>0)puts("ERR"); else if(ans==res)puts("Yes"); else puts("No"); New: ; } return 0; }

 

转载于:https://www.cnblogs.com/Elfish/p/7875433.html

你可能感兴趣的文章
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
Laravel 的生命周期
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
Entity Framework 4.3.1 级联删除
查看>>
学习进度
查看>>
github.com加速节点
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
ATMEGA16 IOport相关汇总
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>