Instant Complexity
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 7
Problem 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
Source
PKU
恶心的模拟加上栈的应用。要模拟代数式的计算,开始我想的太麻烦了,以为要记录乘法和加法甚至括号,想的我头都大了,后来发现每次end结束时都把乘法解决掉了,只要在每次end之前把括号里的因式合并,在乘上括号前的乘数即可。
我先建了一个factor结构体,包含每个因式的系数和次数,如果次数相同就代表可加,否则不可加,乘以n就等于括号里的每项次数都加一,乘以常数就相当于括号里的每项的系数都乘以常数。
1 #include <iostream> 2 #include <cstring> 3 #include <stdio.h> 4 #include <string> 5 #include <algorithm> 6 using namespace std; 7 8 char cmd[1000][6]; 9 int mul[20]; 10 11 struct factor 12 { 13 int coef,time; 14 }fac[40]; 15 16 bool cmp(const factor f1,const factor f2) 17 { 18 return f1.time>f2.time||f1.time==f2.time&&f1.coef>f2.time; 19 } 20 21 int main() 22 { 23 // freopen("in.txt","r",stdin); 24 int T,cas=0; 25 scanf("%d",&T); 26 while(T--) 27 { 28 cas++; 29 int hn=1,tn=0,tot=0,i,j,k,rear1=0,rear2=0; 30 memset(fac,0,sizeof(fac)); 31 memset(mul,0,sizeof(mul)); 32 while(hn!=tn) //当左右括号数相等时输入结束 33 { 34 scanf("%s",cmd[tot]); 35 if(strcmp(cmd[tot],"LOOP")==0) 36 hn++; 37 else if(strcmp(cmd[tot],"END")==0) 38 tn++; 39 tot++; 40 } 41 // for(i=0;i<tot;i++) 42 // cout<<cmd[i]<<endl; 43 for(i=0;i<tot;i++) 44 { 45 if(cmd[i][0]=='O') 46 { 47 if(cmd[i+1][0]=='n') 48 { 49 fac[rear1].coef=1; 50 fac[rear1].time=1; 51 rear1++; 52 } 53 else 54 { 55 int tmp=0; 56 for(j=0;j<strlen(cmd[i+1]);j++) 57 tmp=tmp*10+(cmd[i+1][j]-'0'); 58 fac[rear1++].coef=tmp; 59 // cout<<tmp<<endl; 60 } 61 } 62 else if(cmd[i][0]=='L') 63 { 64 fac[rear1++].coef=-1; //作为分界线,表明括号的开始 65 if(cmd[i+1][0]=='n') 66 mul[rear2++]=-1; //以-1代表乘数是n 67 else 68 { 69 int tmp=0; 70 for(j=0;j<strlen(cmd[i+1]);j++) 71 tmp=tmp*10+(cmd[i+1][j]-'0'); 72 mul[rear2++]=tmp; 73 // cout<<tmp<<endl; 74 } 75 } 76 else if(cmd[i][0]=='E') 77 { 78 int t=rear1-1; 79 rear2--; 80 while(fac[t].coef!=-1&&t!=-1) 81 { 82 t--; 83 } 84 for(j=t+1;j<rear1-1;j++) 85 { 86 if(fac[j].coef==0) 87 continue; 88 for(k=j+1;k<rear1;k++) 89 { 90 if(fac[k].coef==0) 91 continue; 92 if(fac[j].time==fac[k].time) //合并同类项 93 { 94 fac[j].coef+=fac[k].coef; 95 fac[k].coef=0; 96 fac[k].time=0; 97 } 98 } 99 } 100 if(rear2==-1) //表明运行到最后一个end了,跳出 101 break; 102 fac[t].coef=0; //消除分界线 103 int m=mul[rear2]; 104 if(m==-1) 105 { 106 for(j=t+1;j<rear1;j++) 107 { 108 if(fac[j].coef) 109 fac[j].time++; 110 } 111 } 112 else 113 { 114 for(j=t+1;j<rear1;j++) 115 { 116 if(fac[j].coef) 117 fac[j].coef*=m; 118 } 119 } 120 } 121 } 122 sort(fac,fac+rear1,cmp); 123 // for(i=0;i<rear1;i++) 124 // cout<<fac[i].coef<<' '<<fac[i].time<<endl; 125 printf("Program #%d ",cas); 126 printf("Runtime = "); 127 if(fac[0].coef==0) 128 { 129 printf("0 "); 130 continue; 131 } 132 for(i=0;i<rear1;i++) 133 { 134 if(fac[i].time==0&&fac[i].coef==0) 135 break; 136 if(fac[i].coef==1&&fac[i].time==0) 137 printf("%d",fac[i].coef); 138 if(fac[i].coef>1) 139 { 140 printf("%d",fac[i].coef); 141 if(fac[i].time>0) 142 printf("*"); 143 } 144 if(fac[i].coef>0&&fac[i].time>0) 145 { 146 printf("n"); 147 if(fac[i].time>1) 148 printf("^%d",fac[i].time); 149 } 150 if(fac[i+1].coef!=0) 151 printf("+"); 152 } 153 printf(" "); 154 } 155 return 0; 156 }