Instant Complexity - POJ1472
Instant Complexity
| Time Limit: 1000MS | Memory Limit: 10000K |
|---|
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 = 0的时候。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct node
{
int x;
int m;
node *next;
}Node;
void Mult(Node *Head)
{
Node *p;
p=Head;
while(p)
{
p->m++;
p=p->next;
}
}
void Mult(Node *Head,int ans)
{
Node *p;
p = Head;
while(p)
{
p->x*=ans;
p=p->next;
}
}
void Output(Node *Head)
{
Node *p;
p=Head;
bool flag=false;
while(p)
{
if(flag)
{
printf("+");
}
if(p->m==0&&p->x)
{
printf("%d",p->x);
flag=true;
}
else if(p->m==1&&p->x)
{
if(p->x!=1)
printf("%d*n",p->x);
else
{
printf("n");
}
flag=true;
}
else if(p->m>1&&p->x)
{
if(p->x!=1)
printf("%d*n^%d",p->x,p->m);
else
{
printf("n^%d",p->m);
}
flag=true;
}
p=p->next;
}
if(!flag)
{
printf("0");
}
printf("\n");
}
Node * Add(Node *Head,Node *ans)
{
Node *p,*q,*t;
p = ans;
while(p)
{
q = Head;
while(q)
{
if(p->m==q->m)
{
q->x+=p->x;
break;
}
else
{
q=q->next;
}
}
if(!q)
{
t=p->next;
p->next = Head;
Head = p;
p = t ;
}
else
{
p=p->next;
}
}
return Head;
}
Node * Qsort(Node *Head)
{
Node *p,*q,*t;
p = Head;
while(p)
{
t=p;
q=p->next;
while(q)
{
if(q->m==p->m)
{
p->x+=q->x;
t->next = q->next;
q=t->next;
}
else
{
t=t->next;
q=q->next;
}
}
p=p->next;
}
p =Head;
while(p)
{
q=p->next;
while(q)
{
if(p->m<q->m)
{
swap(p->m,q->m);
swap(p->x,q->x);
}
q=q->next;
}
p=p->next;
}
return Head;
}
int Trans(char *s)
{
int ans = 0;
for(int i=0;s[i]!='\0';i++)
{
ans = ans*10+s[i]-'0';
}
return ans;
}
Node * dfs()
{
Node *Head,*ans,*p;
char str[110],c[110];
Head = NULL;
while(1)
{
scanf("%s",str);
if(str[0]=='L')
{
scanf("%s",c);
int num = -1;
if(c[0]!='n')
{
num = Trans(c);
}
ans = dfs();
if(num==-1)
{
Mult(ans);
}
else if(num!=0)
{
Mult(ans,num);
}
else
{
ans = NULL;
}
Head = Add(Head,ans);
}
else if(str[0]=='O')
{
scanf("%s",c);
int num = Trans(c);
if(num==0)
{
continue;
}
p = new Node;
p->x = num;
p->m = 0 ;
p->next = NULL;
Head = Add(Head,p);
}
else if(str[0]=='E')
{
break;
}
}
return Head;
}
int main()
{
char str[110];
int T; int z = 1;
Node *Head;
scanf("%d",&T);
while(T--)
{
Head = NULL;
while(scanf("%s",str)&&strcmp(str,"BEGIN")!=0);
Head = dfs();
Qsort(Head);
printf("Program #%d\n",z++);
printf("Runtime = ");
Output(Head);
printf("\n");
}
return 0;
}
/*
11
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
BEGIN
LOOP 0 OP 17 END
END
BEGIN
LOOP n OP 0 END
END
BEGIN
OP 1 LOOP n LOOP n OP 3 LOOP n OP 2 END END END
END
BEGIN
LOOP n OP 1
LOOP n OP 2
LOOP n OP 3
LOOP n OP 4
LOOP n OP 5
LOOP n OP 6
LOOP n OP 7
LOOP n OP 8
LOOP n OP 9
LOOP n OP 10
END END END END END
LOOP 17 LOOP n LOOP n OP 3 END END END
END END END END END
END
BEGIN LOOP 1 LOOP 2 LOOP 3 OP 1 LOOP 4 LOOP 5 LOOP 6 LOOP 7 LOOP 8 OP 1
END END END END OP 2 END END END END OP 17 END
BEGIN OP 1 OP 2 OP 3 OP 4 OP 5 OP 6 OP 7 OP 8 OP 9 OP 10 OP 11 OP 12 END
BEGIN LOOP n LOOP n LOOP n LOOP n LOOP n LOOP n LOOP n LOOP n LOOP n LOOP n
OP 12345 END END END END END END END END END END END
BEGIN OP
17
LOOP 2
LOOP n
LOOP
2 OP
4 LOOP
n OP 4
LOOP n OP 5
END
END OP 4 END
END END
END
BEGIN
OP 0 LOOP n LOOP
n
OP 88 OP 0 LOOP n LOOP 0 OP 17 END END
END END
OP 0 LOOP n LOOP 3 OP 0 END
END OP 8
END
Sample Output
Program #1
Runtime = 3*n^2+11*n+17
Program #2
Runtime = n^2+1997
Program #3
Runtime = 0
Program #4
Runtime = 0
Program #5
Runtime = 2*n^3+3*n^2+1
Program #6
Runtime = 10*n^10+9*n^9+8*n^8+58*n^7+6*n^6+5*n^5+4*n^4+3*n^3+2*n^2+n
Program #7
Runtime = 40391
Program #8
Runtime = 78
Program #9
Runtime = 12345*n^10
Program #10
Runtime = 20*n^3+16*n^2+32*n+17
Program #11
Runtime = 88*n^2+8
*/
Instant Complexity - POJ1472的更多相关文章
- POJ 1472:Instant Complexity 模拟时间复杂度
Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1908 Accepted: 658 ...
- Instant Complexity(模拟,递归)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1535 Accepted: 529 Description Analyz ...
- POJ 1472 Instant Complexity 应该叫它编程题。。
题目:http://poj.org/problem?id=1472 这个题目是分到“模拟题”一类的,我觉得模拟的成分比较少,主要考察编程能力.独立写完这个题特别兴奋...所以我必须好好说一说,独家哦. ...
- UVA 586 Instant Complexity
给出一段程序,求运行时间. 现在只考虑一层LOOP,不妨用数组a[i]来表示n的i次方的系数.如果输入OP m,那么就在a[0]上加m,遇到END,就说明循环结束了,需要在系数上乘以循环次数.如果次数 ...
- 三部曲二(基本算法、动态规划、搜索)-1004-Instant Complexity
Instant Complexity Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
- poj1472[模拟题]
Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2017 Accepted: 698 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- Instant Radiosity实现
本来说等把课程作业做完再来弄这个,但是还是没有忍住,先做了,主要原因还是这个算法很容易实现.这个算法在1997年由Keller首次提出.虽然名字叫Instant Radiosity,但是它和Radio ...
随机推荐
- sshpass----------------sshfs--sftp(sublime)
源码下载地址:http://sourceforge.net/projects/sshpass/ tar -zxvf sshpass-1.05.tar.gz cd sshpass-1.05 ./co ...
- 视觉机器学习------K-means算法
K-means(K均值)是基于数据划分的无监督聚类算法. 一.基本原理 聚类算法可以理解为无监督的分类方法,即样本集预先不知所属类别或标签,需要根据样本之间的距离或相似程度自动进行分类.聚 ...
- Linq to Sql : 并发冲突及处理策略
原文:Linq to Sql : 并发冲突及处理策略 1. 通过覆盖数据库值解决并发冲突 try { db.SubmitChanges(ConflictMode.ContinueOnConflict) ...
- 《Linux及安全》期中总结&《Linux内核分析》期终总结
[5216 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK NINE ...
- Android开发环境配置
由于公司项目需要,最近转做Android开发,这里我来介绍一下Android开发环境的配置过程. 首先,需要下载所需要的软件工具,如下所示: 1.Java:开发基础环境,JDK和JRE这两个都要下载的 ...
- 基于HTML5和JS实现的切水果游戏
切水果游戏曾经是一款风靡手机的休闲游戏,今天要介绍的就是一款网页版的切水果游戏, 由JavaSript和HTML5实现,虽然功能和原版的相差太大,但是基本的功能还是具备了,还是模仿的挺逼真,有一定的J ...
- android.graphic.Path
类path是一个封装的几何学路径包括直线,二次曲线,三次曲线.它可以通过函数canvas.drawPath(path, paint)画出来,可以通过填充方式或者画线方式(由paint的style决定) ...
- 最简单的基于JSP标准标签库的增删改查
创建数据库中的表:CREATE TABLE `websites` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL DE ...
- Android中处理崩溃异常和记录日志
大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...
- Microsoft Visual Studio 2015激活密匙
企业版:http://download.microsoft.com/download/B/8/F/B8F1470D-2396-4E7A-83F5-AC09154EB925/vs2015.ent_chs ...