Poj1472 -- Instant Complexity (simulation), complexity

Source: Internet
Author: User

Poj1472 -- Instant Complexity (simulation), complexity
Instant ComplexityTime Limit:1000 MSMemory Limit:10000KB64bit IO Format:% I64d & % I64uSubmit Status

 

Appoint description: System Crawler)

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 algorithm that takes quadratic time for the same task, and thus shocould be preferred.

Generally, one determines the run-time of an algorithm in relation to the 'SIZE' n of the input, which cocould 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 wocould be great if this cocould 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 runtime 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 abve. 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 n    OP 4    LOOP 3      LOOP n        OP 1      END      OP 2    END    OP 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

 

 

Computing time complexity: only the execution statement and LOOP are used. The execution statement OP is followed by a non-negative integer, And the LOOP is followed by a non-negative integer or n to calculate the complexity of the entire program.

Create a stack to record the current complexity. If no OP is encountered, calculate the current complexity and accumulate it. Control output,

 

 

#include <cstdio>#include <cstring>#include <algorithm>#include <stack>using namespace std ;stack <int> sta ;int c[15] , a , b ;//char str[10] ;int main(){    int t , tt , temp , k , i , j , l ;    scanf("%d", &t) ;    for( tt = 1 ; tt <= t ; tt++ )    {        while( !sta.empty() ) sta.pop() ;        memset(c,0,sizeof(c)) ;        a = 1 ;        b = 0 ;        temp = 0 ;        scanf("%s", str) ;        while( temp >= 0 )        {            scanf("%s", str) ;            if( strcmp(str,"LOOP") == 0 )            {                temp++ ;                scanf("%s", str) ;                if( str[0] == 'n' )                {                    b++ ;                    sta.push(-1) ;                }                else                {                    k = 0 ;                    l = strlen(str) ;                    for(i = 0 ; i < l ; i++)                    {                        k = k*10 + str[i] - '0' ;                    }                    if( k == 0 )                        sta.push(a) ;                    a *= k ;                    sta.push(k) ;                }            }            else if( strcmp(str,"OP") == 0 )            {                scanf("%s", str) ;                k = 0 ;                l = strlen(str) ;                for(i = 0 ; i < l ; i++)                    k = k*10 + str[i] - '0' ;                c[b] += k*a ;            }            else            {                temp-- ;                if( temp == -1 )                    continue ;                if( sta.top() == 0 )                {                    sta.pop() ;                    a = sta.top() ;                    sta.pop() ;                }                else if( sta.top() == -1 )                {                    sta.pop() ;                    b-- ;                }                else                {                    a /= sta.top() ;                    sta.pop() ;                }            }        }        printf("Program #%d\n", tt) ;        printf("Runtime = ") ;        int flag = 0 ;        for(i = 14 ; i >= 0 ; i--)        {            if( c[i] == 0 ) continue ;            if( flag )                printf("+") ;            if( i == 0 )            {                printf("%d", c[i]);            }            else            {                if( c[i] > 1 )                    printf("%d", c[i]) ;                if( i != 0 )                {                    if( c[i] > 1 )                        printf("*");                    printf("n") ;                    if( i > 1 )                        printf("^%d", i) ;                }            }            flag = 1 ;        }        if( flag == 0 )            printf("0") ;        printf("\n") ;        if( tt < t )            printf("\n") ;    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.