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. ios-点击屏幕,隐藏键盘

    ios-点击屏幕,隐藏键盘 - (void)getFirstRegist{ //结束键盘编辑 __weak typeof(self)weakSelf = self; UITapGestureRecog ...

  2. (转载)linux那点事儿(上)

    原文地址:http://www.cnblogs.com/fnng/archive/2012/03/19/2407162.html 本文只是转载供自己学习之用 本文算是学linux的学习笔记吧!其实li ...

  3. Eclipse中文语言包下载

    Kepler .Juno . Indigo语言包: http://www.eclipse.org/babel/downloads.php 其他低版本Eclipse语言包下载: http://archi ...

  4. springmvc xml 空模板

    <?xml version="1.0" encoding="UTF-8"?><!-- Bean头部 --><beans xmlns ...

  5. Oracle --1536错误解决(超出表空间)

    --导入数据库时提示 超出表空间限额,1536错误,解决方法:去除限额. 执行:--alter user username quota unlimited on users; 例: alter use ...

  6. 【css面试题】三个DIV要求水平对齐,左右两个DIV宽度固定为100px,中间那个DIV充满剩余的宽度(至少2种方法)

    这是我在一家公司面试时遇到的问题,当时没有答上来!! 所以看到的小伙伴一定要注意了!! 变化浏览器宽度可看到效果: 左 右 中 然后我们来看看代码: 第一种方法:(浮动) <style type ...

  7. PreferenceFragment界面透明问题

    PreferenceFragment界面默认是透明的 而其布局代码框架为 <PreferenceScreen> ... </PreferenceScreen>,背景色及透明度属 ...

  8. WinForm中的事件触发机制学习

    在一个Form窗体中拖个按钮,双击后系统自动生成代码: private void button1_Click(object sender, EventArgs e) { } 同时在窗体的Initial ...

  9. 【转】IOS AutoLayout详解(三)用代码实现(附Demo下载)

    转载自:blog.csdn.net/hello_hwc IOS SDK详解 前言: 在开发的过程中,有时候创建View没办法通过Storyboard来进行,又需要AutoLayout,这时候用代码创建 ...

  10. const和volatile的区别

    一.关键字const有什么含意? 只要一听到说:“const意味着常数”,就知道我正在和一个业余者打交道.去年Dan Saks已经在他的文章里完全概括了const的所有用法,因此ESP(译者:Embe ...