【POJ】 Instant Complexity (模拟)

【POJ】 Instant Complexity (模拟)

Instant Complexity

Time Limit:1000MSMemory Limit:10000K

Total Submissions:1905Accepted:657

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:< 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.

Sample Input

2BEGIN LOOP nOP 4LOOP 3LOOP nOP 1ENDOP 2ENDOP 1 END OP 17ENDBEGIN OP 1997 LOOP n LOOP n OP 1 END ENDEND

Sample Output

Program #1Runtime = 3*n^2+11*n+17Program #2Runtime = n^2+1997

Source

题目是要求计算一段程序的时间复杂度 LOOP n/x END间循环n/x(数字)次 OP x表示执行x次

递归模拟即可 输出挺恶心。。死在乘号上了……

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;int cn[111][11];char str[111];char tmp[111];int tp;void OutPut(){printf("Runtime = ");int f = 0;for(int i = 10; i >=0; –i){if(!cn[0][i]) continue;if(f) putchar('+');else f = 1;if(cn[0][i] != 1 || !i)printf("%d",cn[0][i]);if(cn[0][i] != 1 && i) putchar('*');if(i > 1) printf("n^%d",i);else if(i) putchar('n');}if(!f) printf("%d",f);puts("");puts("");}int ToNum(char *st){int ans = 0;for(int i = 0; st[i]; ++i){ans = ans*10+st[i]-'0';}return ans;}void dfs(){int ans = 0;int pre = tp++;while(~scanf("%s",str) && str[0] != 'E'){if(str[0] == 'O'){int x;scanf("%d",&x);ans += x;}else{scanf("%s",tmp);if(tmp[0] == 'n'){dfs();for(int i = 10; i; –i) cn[tp][i] = cn[tp][i-1];cn[tp][0] = 0;}else{int x = ToNum(tmp);dfs();for(int i = 0; i <= 10; ++i) cn[tp][i] *= x;}tp++;}}for(int i = pre+1; i < tp; ++i)for(int j = 0; j <= 10; ++j){cn[pre][j] += cn[i][j];cn[i][j] = 0;}tp = pre;cn[pre][0] += ans;}int main(){freopen("in.in","r",stdin);freopen("out.out","w",stdout);int t;scanf("%d",&t);for(int z = 1; z <= t; ++z){memset(cn,0,sizeof(cn));scanf("%s",str);tp = 0;dfs();printf("Program #%d\n",z);OutPut();}return 0;}上个Source好人一生平安……

版权声明:本文为博主原创文章,,未经博主允许不得转载。

三亚呀——赴一个蓝天碧海。只是微笑地固执自己的坚持,

【POJ】 Instant Complexity (模拟)

相关文章:

你感兴趣的文章:

标签云: