Instant Complexity
Instant Complexity |
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 analgorithm 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 soon. Since determining a formula dependent onn for the run-time of an algorithm is no easy task, itwould be great if this could be
automated. Unfortunately, this is not possible in general, but in thisproblem we will consider programs of a very simple nature, for which it is possible. Our programsare 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-statementcosts as many time-units as its parameter specifies. The statement list enclosed by a LOOP-statementis executed as many times as the parameter of the statement indicates, i.e., the given constant numberof times, if a number is given, and n times, ifn is given. The run-time of a statement list is the sum ofthe times of its constituent parts. The total run-time therefore generally depends onn.
Input
The input file starts with a line containing the number k of programs in the input. Following this arek programs which are constructed according to the grammar given above. Whitespace and newlinescan appear anywhere in a program, but not within the keywords BEGIN, END, LOOP and OP or in aninteger 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 sampleoutput. Then output the run-time of the program in terms ofn; this will be a polynomial of degree Ÿ
Runtime =
a*n^10+
b*n^9+ . . . +
i*n^2+
j*n+k
'', where terms with zero coefficients
are leftout, and factors of 1 are not written. If the runtime is zero, just print ``Runtime = 0''.
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
LOOP表示循环开始,END表示循环结束,OP是常数的复杂度。计算总的复杂度。
用递归,数组a为当前循环体的复杂度的系数(a[i]为n^i的系数)。如果是OP,直接在a[0]上加这个复杂度,如果是LOOP,用数组b记录这个内部循环的复杂度,在a中对应位置加上。递归时还需要一个参数p,当前循环体循环的次数,最后,如果p如果不是n,就把a[i]的每个位置乘上这个值,如果是n,就把a[i]每个位置向右移动一位,并把a[0]设为0。
注意输出如果是1的地方就不要输出。如果答案是0要输出0。
#include<cstring>
#include<cstdio>
#include<iostream>
#include<climits>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#define INF 0x3f3f3f3f
#define MAXN 15
#define MAXM 30010
#define pii pair<int,int>
using namespace std;
int T,ans[MAXN];
void solve(int *a,int p){
char str[10],str2[10];
int t;
while(scanf("%s",str),str[0]!='E'){
if(str[0]=='O'){
scanf("%d",&t);
a[0]+=t;
}
if(str[0]=='L'){
scanf("%s",str2);
if(str2[0]=='n') t=-1;
else sscanf(str2,"%d",&t);
int b[MAXN]={0};
solve(b,t);
for(int i=0;i<MAXN;i++) a[i]+=b[i];
}
}
if(p==-1){
for(int i=MAXN-1;i>0;i--) a[i]=a[i-1];
a[0]=0;
}
else for(int i=MAXN-1;i>=0;i--) a[i]*=p;
}
void print(){
int first=1;
for(int i=MAXN-1;i>0;i--) if(ans[i]){
if(first) first=0;
else printf("+");
if(ans[i]!=1) printf("%d*",ans[i]);
printf("n");
if(i!=1) printf("^%d",i);
}
if(ans[0]){
if(first) first=0;
else printf("+");
printf("%d",ans[0]);
}
if(first) printf("0");
printf("\n\n");
}
int main(){
freopen("in.txt","r",stdin);
int cas=0;
scanf("%d",&T);
while(T--){
printf("Program #%d\nRuntime = ",++cas);
char str[10];
scanf("%s",str);
memset(ans,0,sizeof(ans));
solve(ans,1);
print();
}
return 0;
}