【POJ】 Instant Complexity (模拟)

【POJ】 Instant Complexity (模拟)

Instant Complexity

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1905   Accepted: 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

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

Southwestern European Regional Contest 1997

题目是要求计算一段程序的时间复杂度 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 http://www.informatik.uni-ulm.de/acm/Regionals/1997/ 好人一生平安……

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

时间: 2025-01-12 02:45:38

【POJ】 Instant Complexity (模拟)的相关文章

UVA - 586 Instant Complexity

Description  Instant Complexity  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 analgorithm that takes quad

[ACM] POJ 1068 Parencodings(模拟)

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn

【POJ 1028】模拟浏览器

本题要求模拟浏览器的前进.后退等操作. 用两个栈实现,一个控制前进,一个控制后退. 在前进与后退操作中,从一个栈中弹出栈顶元素,压入另一个栈中. 当打开一个新网页时,将前进栈清空. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> #include

poj1472--Instant Complexity(模拟)

Instant Complexity Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2013-03-03) Description Analyzing the run-time complexity of algorithms is an important tool for desi

POJ 1835 大模拟

宇航员 #include<iostream> #include<cstdio> #include<string> #include<cstring> #define maxn 10010 using namespace std; int a[7],temp[7]; char str[10]; void solve(int str2[],int str3[]) { if(strcmp(str,"forward")==0)//方向不变 { s

POJ 1102 LC-Display 模拟

Description A friend of you has just bought a new computer. Until now, the most powerful computer he ever used has been a pocket calculator. Now, looking at his new computer, he is a bit disappointed, because he liked the LC-display of his calculator

UVA586 - Instant Complexity(递归加模拟)

题目链接 题目大意:给你一段代码,要求你算复杂度.OP代表操作,Loop代表循环,end结束. 解题思路:递归去模拟.具体看代码. 代码: #include <cstdio> #include <cstring> const int N = 15; char s1[N], s2[N]; typedef long long ll; ll nv[N]; void solve (ll * v) { while (1) { scanf ("%s", s1); int n

poj 2160 Box 模拟

题意: 给6块木板的长度,判断它们是否可以组成一个长方体. 分析: 模拟,首先判断6块木板是否可以分为3组完全相等的木板,再判断这3组木板是否能组成长方体. 代码: //poj 2160 //sep9 #include <iostream> #include <algorithm> using namespace std; int vis[8]; int x[8],y[8]; int a[8]; int main() { int i,j; for(i=0;i<6;++i){

poj 1021 2D-Nim 模拟

题意: 给两个平面,每个平面上有一些点,相邻的点可构成点集,为两个平面内的点集是够都对应相似.两个点集相似是指经过对称或旋转或平移后相等. 分析: 直接模拟判断. 代码: //poj 1021 //sep9 #include <iostream> #include <vector> #include <algorithm> using namespace std; int w,h,n; int g[128][128]; int vis[128][128]; int di