Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1535   Accepted: 529

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 简直是想半天也无从下手的一道题,不过个人感觉通过这道题对递归了解的更深刻了点,不过自己写递归,还是不会写。。。sad.加油吧

大致题意:

给出一段Pascial程序,计算其时间复杂度(能计算的项则计算,不能计算则化到最简的关于n的表达式O(n),并把各项根据n的指数从高到低排列),输出时,系数为0的项不输出,系数为1的项不输出系数,指数为1的项不输出指数。

一段程序只有唯一一个BEGIN,代表程序的开始。与其对应的为最后的END,代表程序的结束。

一段程序最多只有10层循环嵌套,循环的入口为LOOP,一个LOOP对应一个END,代表该层循环的结束。

一段程序中OP的个数不限。

LOOP是循环的入口,其后面的数据可能是常量(非负整数),也可能是变量n,代表循环体执行的次数。

OP是语句,其后面的数据只能为常量(非负整数),代表该语句执行的次数。

还要注意输出,指数为1的不输出指数,系数为1的不输出系数,系数为0的不输出,指数为0的只输出常数

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
void Loop(int *ans, char *LpNum)//LpNum代表该循环的次数
{
char s[],s1[] = {};
while(scanf("%s",s) && s[] != 'E')
{
if(s[] == 'L')//相当于一个大循环嵌套的小循环,是一个子结构,所以,递归调用
{
int *tmp = new int [];
memset(tmp,,*sizeof(int));//暂存数组tmp
scanf("%s",s1);
Loop(tmp,s1); for(int i = ; i <= ; i++)
ans[i] += tmp[i];
}
else if(s[] == 'O')
{
int x;
scanf("%d",&x);
ans[] += x;
}
} if(LpNum[] == 'n')
{
for(int i = ; i > ; i--)
ans[i] = ans[i-];
ans[] = ;
}
else
{
int x = atoi(LpNum);
for(int i = ; i <= ; i++)
ans[i] *= x;
}
} int main()
{
int test;
int ans[];
scanf("%d",&test);
for(int item = ; item <= test; item++)
{
char s[];
scanf("%s",s);
memset(ans,,sizeof(ans)); Loop(ans,"");//把最外面的大循环次数当做1 printf("Program #%d\n",item);
printf("Runtime = "); bool tag = ;
for(int i = ; i >= ; i--)
{
if(ans[i] == )
continue;
if(i == )
printf("%s%d",tag?"":"+",ans[]);
else if(ans[i] == )
printf("%sn",tag?"":"+");
else if(ans[i] > )
printf("%s%d*n",tag?"":"+",ans[i]);
if(i > )
printf("^%d",i);
tag = ;
}
if(tag)
printf("");
printf("\n\n"); }
return ;
}
												

Instant Complexity(模拟,递归)的更多相关文章

  1. POJ 1472:Instant Complexity 模拟时间复杂度

    Instant Complexity Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1908   Accepted: 658 ...

  2. Instant Complexity - POJ1472

    Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Description Analyzing the run-time comple ...

  3. python--递归(附利用栈和队列模拟递归)

    博客地址:http://www.cnblogs.com/yudanqu/ 一.递归 递归调用:一个函数,调用的自身,称为递归调用 递归函数:一个可以调用自身的函数称为递归函数 凡是循环能干的事,递归都 ...

  4. 深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)

    将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...

  5. 【ACwing 93】【模版】非递归实现组合型枚举——模拟递归

    (题面来自ACwing) 从 1~n 这 n 个整数中随机选出 m 个,输出所有可能的选择方案. 输入格式 两个整数 n,m ,在同一行用空格隔开. 输出格式 按照从小到大的顺序输出所有方案,每行1个 ...

  6. POJ 1472 Instant Complexity 应该叫它编程题。。

    题目:http://poj.org/problem?id=1472 这个题目是分到“模拟题”一类的,我觉得模拟的成分比较少,主要考察编程能力.独立写完这个题特别兴奋...所以我必须好好说一说,独家哦. ...

  7. 【模拟+递归+位运算】POJ1753-Flip Game

    由于数据规模不大,利用爆搜即可.第一次用位运算写的,但是转念一想应该用递归更加快,因为位运算没有剪枝啊(qДq ) [思路] 位运算:时间效率较低(172MS),有些辜负了位运算的初衷.首先将二维数组 ...

  8. UVA 586 Instant Complexity

    给出一段程序,求运行时间. 现在只考虑一层LOOP,不妨用数组a[i]来表示n的i次方的系数.如果输入OP m,那么就在a[0]上加m,遇到END,就说明循环结束了,需要在系数上乘以循环次数.如果次数 ...

  9. Atcoder Beginner Contest 115 D Christmas 模拟,递归 B

    D - Christmas Time limit : 2sec / Memory limit : 1024MB Score : 400 points Problem Statement In some ...

随机推荐

  1. shell入门之函数应用 分类: 学习笔记 linux ubuntu 2015-07-10 21:48 77人阅读 评论(0) 收藏

    最近在学习shell编程,文中若有错误的地方还望各位批评指正. 先来看一个简单的求和函数 #!/bin/bash #a test about function f_sum 7 8 function f ...

  2. ip、数字的互转

    # ip ==> 数字 >>> ip2num = lambda x:sum([256**j*int(i) for j,i in enumerate(x.split('.')[: ...

  3. linux下sed命令笔记

    sed 流编辑器 Stream EDitor三大文本处理工具:grep,sed,awk 语法:sed 'AddressCommand' file ...Address:    1,StartLine, ...

  4. Android学习笔记(广播机制)

    1.Android的广播机制介绍 收听收音机也是一种广播,在收音机中有很多个广播电台,每个广播电台播放的内容都不相同.接受广播时广播(发送方)并不在意我们(接收方)接收到广播时如何处理.好比我们收听交 ...

  5. 读懂IL代码(二)

    上一篇提到了最基本的IL代码,应该是比较通俗易懂的,所以有了上一篇的基础之后,这篇便要深入一点点的来讲述了. 首先我必须再来说一些重要的概念: Evaluation Stack(评估栈):这是由.NE ...

  6. IE6解决固定定位代码

    有些朋友在进行网页布局时,会遇到IE6浏览器不支持固定定位的兼容性问题,本博将详细介绍此问题的解决方法,需要了解的朋友可以参考下. ie6 垂直居中固定定位,代码如下: #center {_posit ...

  7. SOAPUI请求及mockservice 使用

    1.新建soap Project,输入wsdl的地址,运行request                                                               2 ...

  8. 【BZOJ1500】【块状链表】维修数列

    Description Input 输入文件的第1行包含两个数N和M,N表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一条命令,格式参见问题描述 ...

  9. 【BZOJ1012】【树状数组求区间最值】最大数maxnumber

    Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ...

  10. centos7 开机启动某些程序的方法

    针对svn,nginx每次重启后均要手工启动,好麻烦,所以考虑将其做成开机启动,做成服务好麻烦,考虑像windows 一样,放在某个启动项中完成. 打开启动文件后,发现里面文件内容如下: #!/bin ...