poj 1472 Instant Complexity

Instant Complexity
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2056 Accepted: 710

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

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

提示

题意:

BEGIN是函数开始,END表示函数和循环体结束,OP(后面的值不会有n,都是已知数)可以看做+x,LOOP(后面的值会有n和已知数,循环嵌套最多为10层)表示循环体的开始。请按照样例输出函数的值。

思路:

用数组存储每个n不同指数形式的系数,每嵌套一个LOOP n,n的指数就会增加1,OP x是增加系数,由此可以得出kn+b这样的形式。要注意OP 0,LOOP 0以及sum=0的形式。

示例程序

Source Code

Problem: 1472		Code Length: 2398B
Memory: 384K		Time: 0MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
struct
{
    int a;
}ans[11];
int top;
void f(char s[],int deep,int n)
{
    int i,n1=0,m;
    char ch[20];
    if(deep>top)				//最高指数记录
    {
        top=deep;
    }
    if(s[0]!='n')				//系数不是n需要化成int来计算
    {
        for(i=0;s[i]!='\0';i++)
        {
            n1=n1*10+(s[i]-'0');
        }
        n=n*n1;
    }
    while(scanf("%s",ch)!=EOF&&strcmp(ch,"END")!=0)		//循环体
    {
        if(strcmp(ch,"OP")==0)
        {
            scanf("%d",&m);
            ans[deep].a=ans[deep].a+m*n;
        }
        else
        {
            scanf("%s",ch);
            if(ch[0]!='n')
            {
                f(ch,deep,n);
            }
            else
            {
                f(ch,deep+1,n);
            }
        }
    }
}
int main()
{
    int i,i1,t,m,flag;
    char ch[20];
    scanf("%d",&t);
    for(i=1;t>=i;i++)
    {
        memset(ans,0,sizeof(ans));
        top=0;
        flag=0;
        scanf("%s",ch);
        while(scanf("%s",ch)!=EOF&&strcmp(ch,"END")!=0)			//循环体
        {
            if(strcmp(ch,"OP")==0)
            {
                scanf("%d",&m);
                ans[0].a=ans[0].a+m;			//系数增加
            }
            else
            {
                scanf("%s",ch);
                if(ch[0]!='n')				//LOOP x(x为已知数),指数不变系数扩大x倍
                {
                    f(ch,0,1);
                }
                else					//LOOP n,指数上升1
                {
                    f(ch,1,1);
                }
            }
        }
        printf("Program #%d\nRuntime = ",i);
        for(i1=top;i1>=0;i1--)
        {
            if(ans[i1].a==0)				//系数为0,n^i1
            {
                continue;
            }
            if(flag==1)					//不是第一项,要输出'+'
            {
                printf("+");
            }
            if(i1>1)					//指数为1以上
            {
                if(ans[i1].a!=1)
                {
                    printf("%d*n^%d",ans[i1].a,i1);
                }
                else
                {
                    printf("n^%d",i1);
                }
            }
            else if(i1==1)				//指数为1
            {
                if(ans[i1].a!=1)
                {
                    printf("%d*n",ans[i1].a);
                }
                else
                {
                    printf("n");
                }
            }
            else					//常数
            {
                printf("%d",ans[i1].a);
            }
            flag=1;
        }
        if(flag==0)					//sum=0需要输出0
        {
            printf("0");
        }
        printf("\n\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值