Instant Complexity
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2081 | Accepted: 718 |
Description
Analyzing the run-time complexity of algorithms is an important tool for designing efficient programs that solve a problem. An algorithm that runs in linear time is usually much faster than an algorithm that takes quadratic time for the same task, and thus should be preferred.
Generally, one determines the run-time of an algorithm in relation to the `size' n of the input, which could be the number of objects to be sorted, the number of points in a given polygon, and so on. Since determining a formula dependent on n for the run-time of an algorithm is no easy task, it would be great if this could be automated. Unfortunately, this is not possible in general, but in this problem we will consider programs of a very simple nature, for which it is possible. Our programs are built according to the following rules (given in BNF), where < number > can be any non-negative integer:
The run-time of such a program can be computed as follows: the execution of an OP-statement costs as many time-units as its parameter specifies. The statement list enclosed by a LOOP-statement is executed as many times as the parameter of the statement indicates, i.e., the given constant number of times, if a number is given, and n times, if n is given. The run-time of a statement list is the sum of the times of its constituent parts. The total run-time therefore generally depends on n.
Generally, one determines the run-time of an algorithm in relation to the `size' n of the input, which could be the number of objects to be sorted, the number of points in a given polygon, and so on. Since determining a formula dependent on n for the run-time of an algorithm is no easy task, it would be great if this could be automated. Unfortunately, this is not possible in general, but in this problem we will consider programs of a very simple nature, for which it is possible. Our programs are built according to the following rules (given in BNF), where < number > can be any non-negative integer:
< Program > ::= "BEGIN" < Statementlist > "END" < Statementlist > ::= < Statement > | < Statement > < Statementlist > < Statement > ::= < LOOP-Statement > | < OP-Statement > < LOOP-Statement > ::= < LOOP-Header > < Statementlist > "END" < LOOP-Header > ::= "LOOP" < number > | "LOOP n" < OP-Statement > ::= "OP" < number >
The run-time of such a program can be computed as follows: the execution of an OP-statement costs as many time-units as its parameter specifies. The statement list enclosed by a LOOP-statement is executed as many times as the parameter of the statement indicates, i.e., the given constant number of times, if a number is given, and n times, if n is given. The run-time of a statement list is the sum of the times of its constituent parts. The total run-time therefore generally depends on n.
Input
The input starts with a line containing the number k of programs in the input. Following this are k programs which are constructed according to the grammar given above. Whitespace and newlines can appear anywhere in a program, but not within the keywords BEGIN, END, LOOP and OP or in an integer value. The nesting depth of the LOOP-operators will be at most 10.
Output
For each program in the input, first output the number of the program, as shown in the sample output. Then output the run-time of the program in terms of n; this will be a polynomial of degree Y <= 10. Print the polynomial in the usual way, i.e., collect all terms, and print it in the form "Runtime = a*n^10+b*n^9+ . . . +i*n^2+ j*n+k", where terms with zero coefficients are left out, and factors of 1 are not written. If the runtime is zero, just print "Runtime = 0".
Output a blank line after each test case.
Output a blank line after each test case.
Sample Input
2 BEGIN LOOP n OP 4 LOOP 3 LOOP n OP 1 END OP 2 END OP 1 END OP 17 END BEGIN OP 1997 LOOP n LOOP n OP 1 END END END
Sample Output
Program #1 Runtime = 3*n^2+11*n+17 Program #2 Runtime = n^2+1997
题意:告诉你一组代码,然后让你计算这组代码的时间的复杂度。
规则:
1.op是普通的运算语句,时间复杂度为常数咯。op后面带的必定是数字。
2.loop是循环语句,时间复杂度当然是跟层数有关,loop后面跟的可能是数字,也可能是n。
思路:
模拟,QAQ我表示还真的不是很会这种模拟啊,递归不好,可能是思维上抽象能力不够强。
1.当前这一层(一组begin到end)的复杂度为 常数级的复杂度o(n)+循环体的复杂度。
2.循环体的复杂度=循环的次数*(循环体内部的复杂度)。
3.循环体内部的复杂度=第一条所列的。
4.用一个数组来记录当前复杂度的系数。a[0]表示常数级,a[1]表示n的1次方的,a[2]表示n的2次方的........
然后就是根据这个进行一个递归模拟,具体的解释在代码中来说效果更好:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN=10+5;
char str1[MAXN],str2[MAXN];
int ans[MAXN];
//递归模拟
void dfs(int *a,int n)
{
int i;
int m;
//当前层要一直读到end
while(~scanf("%s",str1)&&str1[0]!='E')
{
//如果是op说明当前的语句是常数级的,直接记录下来
if(str1[0]=='O')
{
scanf("%d",&m);
a[0]+=m;
}
//否则的话就是循环体
else
{
scanf("%s",str2);
if(str2[0]=='n')m=-1;//-1表示循环次数为n
else sscanf(str2,"%d",&m);
int b[MAXN];
memset(b,0,sizeof(b));//新开辟数组
dfs(b,m);//用来记录当前循环体的复杂度
for(i=0;i<MAXN;++i)a[i]+=b[i];//得出循环体的复杂度之后,加上常数级的
}
}
if(n==-1)//然后系数乘上当前层循环的次数
{
for(i=MAXN-1;i>0;--i)a[i]=a[i-1];
a[0]=0;
}
else for(i=0;i<MAXN;++i)a[i]*=n;
}
//输出函数
void print()
{
int i;
int sum=0;
for(i=MAXN-1;i>=1;--i)
{
if(ans[i])
{
sum++;
if(sum>1)printf("+");
if(ans[i]>1)printf("%d*",ans[i]);
if(i==1)printf("n");
else printf("n^%d",i);
}
}
if(ans[0])
{
sum++;
if(sum>1)printf("+");
printf("%d",ans[0]);
}
if(!sum)printf("0");
printf("\n\n");
}
int main()
{
int t,i;
scanf("%d",&t);
for(i=1;i<=t;++i)
{
scanf("%s",str1);
memset(ans,0,sizeof(ans));
dfs(ans,1);//两个参数表示当前层(begin到end)复杂度系数的数组,当前层循环的次数
printf("Program #%d\nRuntime = ",i);
print();
}
return 0;
}
至于这里为什么是记录当前层的循环次数,因为我们计算的是当前层里面的语句的复杂度,而循环语句早已经读了。
QAQ让我自己做,估计一个月都不知道怎么做,还是看了一下别人的思路,想了一晚上,才想明白为什么可以这么做TAT感觉自己是一条咸鱼了。