Instant Complexity
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2017   Accepted: 698

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

大致题意:

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

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

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

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

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

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

解题思路:递归+模拟

此题就是一条表达式化简的模拟题,用递归直接模拟。

以第一个样例说明处理方法:

BEGIN

LOOP n

OP 4

LOOP 3

LOOP n

OP 1

END

OP 2

END

OP 1

END

OP 17

END

从该例子我们可以得到一条关于n的最初表达式:

n*(4+3*(n*1+2)+1)+17

稍微化简一下,合并同一层的OP值,得到了

n*(3*(n*1+2)+5)+17

不难看出每一个循环体都能写成k*n+i形式的子表达式,其中loop*关系,op+关系

由于最大循环次数为10,那么我们用exp[11]存储多项式的每一项的指数i和系数k=exp[i],其中exp[0]其实就是常数项,由OP语句产生

注意LOOP后面可能输入字符n,也可能输入数字,处理方法:用字符串输入s,若为s[0]==n,则直接作字符处理;若s[0]!=n,则认为是数字串,把它转换为int再处理。

AC代码:

#include<cstdio>
#include<cstdlib>
using namespace std;
char ch[];
int deal(int *exps){
scanf("%s",ch);
if(ch[]=='E') return ;
if(ch[]=='B') while(deal(exps));
else if(ch[]=='L'){
int t=-,texps[]={};
scanf("%s",ch);
if(ch[]!='n') t=atoi(ch);
while(deal(texps));
if(t==-){
for(int i=;i>=;i--) texps[i]=texps[i-];
texps[]=;
}
else
for(int i=;i<=;i++) texps[i]*=t;
for(int i=;i<=;i++) exps[i]+=texps[i]; }
else{
scanf("%s",ch);
exps[]+=atoi(ch);
return deal(exps);
}
return ;
}
int main(){
int n,t;
scanf("%d",&n);
for(int i=;i<=n;i++){
int exps[]={};t=;
deal(exps);
printf("Program #%d\nRuntime = ", i);
for(int j=;j>=;j--){
if(exps[j]){
t++;
if(t!=) printf("+");
if(exps[j]!=||j==)printf("%d", exps[j]);
if(exps[j]!=&&j>)printf("*");
if(j>)printf("n^%d", j);
if(j==)printf("n");
}
}
if(!t) printf("");
printf("\n\n");
}
return ;
}

poj1472[模拟题]的更多相关文章

  1. poj 1008:Maya Calendar(模拟题,玛雅日历转换)

    Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 D ...

  2. poj 1888 Crossword Answers 模拟题

    Crossword Answers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 869   Accepted: 405 D ...

  3. CodeForces - 427B (模拟题)

    Prison Transfer Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  4. sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)

    The Android University ACM Team Selection Contest Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里 ...

  5. 全国信息学奥林匹克联赛 ( NOIP2014) 复赛 模拟题 Day1 长乐一中

    题目名称 正确答案  序列问题 长途旅行 英文名称 answer sequence travel 输入文件名 answer.in sequence.in travel.in 输出文件名 answer. ...

  6. UVALive 4222 Dance 模拟题

    Dance 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pag ...

  7. cdoj 25 点球大战(penalty) 模拟题

    点球大战(penalty) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/2 ...

  8. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  9. URAL 2046 A - The First Day at School 模拟题

    A - The First Day at SchoolTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...

随机推荐

  1. linux svn使用

    SVN是一种版本管理系统,前身是CVS,是开源软件的基石.即使在沟通充分的情况下,多人维护同一份源代码的一定也会出现混乱的情况,版本管理系统就是为了解决这些问题. SVN中的一些概念 : a. rep ...

  2. UIPanGestureRecognizer

    http://blog.csdn.net/huifeidexin_1/article/details/8282035 UIGestureRecognizer是一个定义基本手势的抽象类,具体什么手势,在 ...

  3. MD5验证工具:md5sum

    linux 下 shell命令 ,制作md5码 也用于软件的md5校验     MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5 全称是报文摘要算法(Message-Digest ...

  4. Windows下sqlmap的使用_01

    环境:win8.1 64位    一.下载 首先,需下载SqlMap以及适用于Windows系统的Python.下载地址如下:   1.1.SqlMap下载地址:https://github.com/ ...

  5. JS基础DOM篇之二:DOM级别与节点层次?

    通过上一篇我们大致了解了什么是DOM,今天我们继续深入了解. 1.DOM级别       在大家阅读DOM标准的时候,可能会看到DOM(0/1/2/3)级的字眼,这就是DOM级别.但实际上,DOM0级 ...

  6. squid添加用户名密码认证

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  7. Flex Alert.show()方法的详解

    本文和大家重点讨论一下Flex Alert.show()flag详细值,Flex Alert.show()里面有多个属性,其中排在第三是flags,这个属性作用是在弹出的Alert提示框里面显示那一个 ...

  8. 关于2000W数据

    前几天在博客园首页看到这个2000W数据的消息,刚好这个学期正在SQL入门,加上好奇心的驱使,把这个下载下来. 一个是600多M的CSV文件,还有一个是1.7G的SQL Server的备份文件,解压后 ...

  9. C++ 不支持模版的分离式编译

    1.C++不支持模版的分离式编译,为什么? C++是分别,单独编译,对于每个cpp文件,预编译为编译单元,这个编译单元是自包含文件,编译的时候,不需要其他的文件,编译好了,生成obj文件,然后连接成e ...

  10. 批量导出表数据到CSV文件

    需求:把oracle数据库中符合条件的N多表,导出成csv文本文件,并以表名.csv为文件名存放. 实现:通过存储过程中UTL_FILE函数来实现.导出的csv文件放入提前创建好的directory中 ...