Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2086 | Accepted: 721 |
Description
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
Output
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
给出一段Pascial程序,计算其时间复杂度(能计算的项则计算,不能计算则化到最简的关于n的表达式O(n),并把各项根据n的指数从高到低排列),输出时,系数为0的项不输出,系数为1的项不输出系数,指数为1的项不输出指数。
一段程序只有唯一一个BEGIN,代表程序的开始。与其对应的为最后的END,代表程序的结束。
一段程序最多只有10层循环嵌套,循环的入口为LOOP,一个LOOP对应一个END,代表该层循环的结束。
一段程序中OP的个数不限。
LOOP是循环的入口,其后面的数据可能是常量(非负整数),也可能是变量n,代表循环体执行的次数。
OP是语句,其后面的数据只能为常量(非负整数),代表该语句执行的次数。
解题思路:
此题就是一条表达式化简的模拟题,用递归直接模拟。
以第一个样例说明处理方法:
BEGIN
LOOP n
OP 4
LOOP 3
LOOP n
OP 1
END
OP 2
END
OP 1
END
OP 17
END
从该例子我们可以得到一条关于n的最初表达式:
n*(4+3*(n*1+2)+1)+17
稍微化简一下,合并同一层的OP值,得到了
n*(3*(n*1+2)+5)+17
不难看出每一个循环体都能写成k*n+i形式的子表达式,其中loop是*关系,op是+关系
//数组a a[0]记录常数项 a[1]记录n^1的系数,a[2]记录n^2的系数……
#include<stdio.h>
#include<string.h>
const int maxn=15;
int ans[maxn];
char str[maxn],s1[maxn];
void dfs(int *a,int n)//递归模拟
{
int i,m;
while(~scanf("%s",s1)&&s1[0]!='E')
{
if(s1[0]=='O')//常数项用a[0]表示
{
scanf("%d",&m);
a[0]+=m;
}
else
{
char s2[maxn];
scanf("%s",s2);
if(s2[0]=='n') m=-1;
else sscanf(s2,"%d",&m);
int b[maxn];
memset(b,0,sizeof(b));
dfs(b,m);
for(i=0;i<maxn;i++)//递归调用结束,其实就是退出了一层循环 此时将各个n对应的几次方的系数相加
a[i]+=b[i];
}
}
if(n==-1)//如果遇到“n”则,其实就是在这层当中每位数乘n 则对应系数的幂加一,就是向前移动一位
{
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 flag=0;
for(i=maxn-1;i>=1;i--)
{
if(ans[i])
{
if(!flag)
flag=1;
else printf("+");
if(ans[i]>1) printf("%d*",ans[i]);
if(i>1) printf("n^%d",i);
if(i==1)printf("n");
}
}
if(ans[0])
{
if(!flag)
flag=1;
else printf("+");
printf("%d",ans[0]);
}
if(!flag)
printf("0");
printf("\n\n");
}
int main()
{
int t,i;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
memset(ans,0,sizeof(ans));
scanf("%s",s1);
dfs(ans,1);
printf("Program #%d\n",i);
printf("Runtime = ");
print();
}
return 0;
}