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. objective c 学习(一)

    问题一:我在程序中看到大量的减号.中括号和NS****这种东西,他们是什么玩意儿? 1 减号(或者加号) 减号表示一个函数.或者方法.或者消息的开始,怎么说都行. 比如c#中,一个方法的写法可能是: ...

  2. Java基础知识强化之集合框架笔记19:List集合迭代器使用之 并发修改异常的产生原因 以及 解决方案

    1. 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. ConcurrentModi ...

  3. UICollectionView reloadData后cell被隐藏

    在使用UICollectionView的页面执行: [self.collectionView reloadData]; 执行后,页面变为空白页,调试发现,执行reloadData 后UICollect ...

  4. 原生JS+tween.js模仿微博发布效果

    转载请注明出处:http://www.cnblogs.com/zhangmingze/p/4816865.html 1.先看效果吧,有效果才有动力: 2.html结构: <!DOCTYPE ht ...

  5. C# DataTable去除重复,极其简便、简单

    其中sourceDT是获取到的一个DataTable类型的集合对象 去重复使用方式: 实例化一个DataView对象 假设为dv,直接dv.ToTable()即可,ToTable中可为(true,&q ...

  6. 在MVC中写Filter时经常filterContext无法代码提示HttpContext的方法和属性的原因

    需要用System.Web.Abstractions.dll HttpContextBase是在System.Web.Abstractions下的,添加对System.Web.Abstractions ...

  7. NSDate,NSCalendar,NSTimer,NSTimeZone

      NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. ...

  8. [转]MySQL数据库备份和还原的常用命令小结

    MySQL数据库备份和还原的常用命令小结,学习mysql的朋友可以参考下: 备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword datab ...

  9. Jquery 点击空白处消失

    $(document).bind("click", function (e){ if ( $((e.target || e.srcElement)).closest("# ...

  10. CListCtrl 的应用

    CMFCListCtrl实现的功能:比较特别的就是通过CImage来扩展每行的高度 CMFCListCtrl m_lisTestResult; 1.插入列(创建表格) m_lisTestResult. ...