您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: