5474. 【NOIP2017提高组正式赛】时间复杂度
2017-11-26 08:35
232 查看
题目描述
小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序,于是你的机会来啦!下面请你编写程序来判断小明对他的每个程序给出的时间复杂度是否正确。A++语言的循环结构如下:
其中“ F i x y ”表示新建变量 i (变量 i 不可与未被销毁的变量重名)并初始化为 x,然后判断 i 和 y 的大小关系,若 i 小于等于 y 则进入循环,否则不进入。每次循环结束后 i 都会被修改成 i +1,一旦 i 大于 y 终止循环。
x 和 y 可以是正整数(x 和 y 的大小关系不定) 或变量 n。n 是一个表示数据规模的变量,在时间复杂度计算中需保留该变量而不能将其视为常数,该数 远大于 100。
“ E ”表示循环体结束。循环体结束时,这个循环体新建的变量也被销毁。
注:本题中为了书写方便,在描述复杂度时,使用大写英文字母“O”表示通常意义下“Θ”的概念。
题目分析
这道题很长,其实很简单,整个程序扫一遍即可。注意细节。
代码
#include<cstdio> #include<cstring> using namespace std; char s[110];int time,len,tot; int a[110],b[110];bool bz[31]; int c[110],d[110],e[110]; int dfs(int l,int r) { if(l>r) return 0; int sum=0; for(int i=l;i<=r;i++) { if(b[i]!=0) { int temp=0; if(c[i]<=d[i]) { temp=dfs(i+1,b[i]-1); if(c[i]!=999999999&&d[i]==999999999) { temp++; } } if(sum<temp) { sum=temp; } i=b[i]; } } return sum; } int main() { int t; scanf("%d",&t); while(t--) { int l; scanf("%d",&l); scanf("%s",s+1); if(s[3]=='1') { time=0; } else { int len2=strlen(s+1);time=0; for(int i=5;i<=len2-1;i++) { time=time*10+s[i]-'0'; } } tot=0;bool bk=true; memset(bz,true,sizeof(bz)); memset(b,0,sizeof(b)); for(int i=1;i<=l;i++) { scanf("%s",s+1); if(s[1]=='F') { tot++; a[tot]=i; scanf("%s",s+1); if(bz[s[1]-96]==true) { e[i]=s[1]-96; bz[s[1]-96]=false; } else a1d3 { bk=false; } scanf("%s",s+1); if(s[1]=='n') { c[i]=999999999; } else { int len2=strlen(s+1);c[i]=0; for(int j=1;j<=len2;j++) { c[i]=c[i]*10+s[j]-'0'; } } scanf("%s",s+1); if(s[1]=='n') { d[i]=999999999; } else { int len2=strlen(s+1);d[i]=0; for(int j=1;j<=len2;j++) { d[i]=d[i]*10+s[j]-'0'; } } } else { if(tot>0) { b[a[tot]]=i; bz[e[a[tot]]]=true; tot--; } else { bk=false; } } } if(tot!=0) { bk=false; } if(bk==false) { printf("ERR\n"); continue; } len=0; if(dfs(1,l)==time) { printf("Yes\n"); } else { printf("No\n"); } } return 0; }
相关文章推荐
- JZOJ 5474. 【NOIP2017提高组正式赛】时间复杂度
- 【NOIP2017DAY2T3 【NOIP2017提高组正式赛】列队 】(动态开点+n棵线段树)
- 【提高组NOIP2017】时间复杂度 题解 分治系统处理
- JZOJ 5473. 【NOIP2017提高组正式赛】小凯的疑惑
- [NOIP2017]时间复杂度(模拟)
- NOIP 2017 提高组 时间复杂度___模拟
- 5475. 【NOIP2017提高组正式赛】逛公园
- 5476. 【NOIP2017提高组正式赛】奶酪
- 【NOIP2017 DAY1T2】 时间复杂度
- JZOJ 5475. 【NOIP2017提高组正式赛】逛公园
- 5477. 【NOIP2017提高组正式赛】宝藏
- NOIP 2017 时间复杂度
- 5473. 【NOIP2017提高组正式赛】小凯的疑惑
- 5478. 【NOIP2017提高组正式赛】列队
- 【NOIP2017】时间复杂度
- JZOJ 5478. 【NOIP2017提高组正式赛】列队
- JZOJ 5477. 【NOIP2017提高组正式赛】宝藏
- 【jzoj5286】【NOIP2017提高A组模拟8.16】【花花的森林 】【时间倒流】
- NOIP2017 D1T2 时间复杂度
- JZOJ 5476. 【NOIP2017提高组正式赛】奶酪