Instant Complexity

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 8   Accepted Submission(s) : 7
Problem 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
PKU
 
 
恶心的模拟加上栈的应用。要模拟代数式的计算,开始我想的太麻烦了,以为要记录乘法和加法甚至括号,想的我头都大了,后来发现每次end结束时都把乘法解决掉了,只要在每次end之前把括号里的因式合并,在乘上括号前的乘数即可。
我先建了一个factor结构体,包含每个因式的系数和次数,如果次数相同就代表可加,否则不可加,乘以n就等于括号里的每项次数都加一,乘以常数就相当于括号里的每项的系数都乘以常数。
 
 #include <iostream>
#include <cstring>
#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std; char cmd[][];
int mul[]; struct factor
{
int coef,time;
}fac[]; bool cmp(const factor f1,const factor f2)
{
return f1.time>f2.time||f1.time==f2.time&&f1.coef>f2.time;
} int main()
{
// freopen("in.txt","r",stdin);
int T,cas=;
scanf("%d",&T);
while(T--)
{
cas++;
int hn=,tn=,tot=,i,j,k,rear1=,rear2=;
memset(fac,,sizeof(fac));
memset(mul,,sizeof(mul));
while(hn!=tn) //当左右括号数相等时输入结束
{
scanf("%s",cmd[tot]);
if(strcmp(cmd[tot],"LOOP")==)
hn++;
else if(strcmp(cmd[tot],"END")==)
tn++;
tot++;
}
// for(i=0;i<tot;i++)
// cout<<cmd[i]<<endl;
for(i=;i<tot;i++)
{
if(cmd[i][]=='O')
{
if(cmd[i+][]=='n')
{
fac[rear1].coef=;
fac[rear1].time=;
rear1++;
}
else
{
int tmp=;
for(j=;j<strlen(cmd[i+]);j++)
tmp=tmp*+(cmd[i+][j]-'');
fac[rear1++].coef=tmp;
// cout<<tmp<<endl;
}
}
else if(cmd[i][]=='L')
{
fac[rear1++].coef=-; //作为分界线,表明括号的开始
if(cmd[i+][]=='n')
mul[rear2++]=-; //以-1代表乘数是n
else
{
int tmp=;
for(j=;j<strlen(cmd[i+]);j++)
tmp=tmp*+(cmd[i+][j]-'');
mul[rear2++]=tmp;
// cout<<tmp<<endl;
}
}
else if(cmd[i][]=='E')
{
int t=rear1-;
rear2--;
while(fac[t].coef!=-&&t!=-)
{
t--;
}
for(j=t+;j<rear1-;j++)
{
if(fac[j].coef==)
continue;
for(k=j+;k<rear1;k++)
{
if(fac[k].coef==)
continue;
if(fac[j].time==fac[k].time) //合并同类项
{
fac[j].coef+=fac[k].coef;
fac[k].coef=;
fac[k].time=;
}
}
}
if(rear2==-) //表明运行到最后一个end了,跳出
break;
fac[t].coef=; //消除分界线
int m=mul[rear2];
if(m==-)
{
for(j=t+;j<rear1;j++)
{
if(fac[j].coef)
fac[j].time++;
}
}
else
{
for(j=t+;j<rear1;j++)
{
if(fac[j].coef)
fac[j].coef*=m;
}
}
}
}
sort(fac,fac+rear1,cmp);
// for(i=0;i<rear1;i++)
// cout<<fac[i].coef<<' '<<fac[i].time<<endl;
printf("Program #%d\n",cas);
printf("Runtime = ");
if(fac[].coef==)
{
printf("0\n\n");
continue;
}
for(i=;i<rear1;i++)
{
if(fac[i].time==&&fac[i].coef==)
break;
if(fac[i].coef==&&fac[i].time==)
printf("%d",fac[i].coef);
if(fac[i].coef>)
{
printf("%d",fac[i].coef);
if(fac[i].time>)
printf("*");
}
if(fac[i].coef>&&fac[i].time>)
{
printf("n");
if(fac[i].time>)
printf("^%d",fac[i].time);
}
if(fac[i+].coef!=)
printf("+");
}
printf("\n\n");
}
return ;
}

三部曲二(基本算法、动态规划、搜索)-1004-Instant Complexity的更多相关文章

  1. LeetCode初级算法--动态规划01:爬楼梯

    LeetCode初级算法--动态规划01:爬楼梯 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...

  2. 算法-动态规划 Dynamic Programming--从菜鸟到老鸟

    算法-动态规划 Dynamic Programming--从菜鸟到老鸟      版权声明:本文为博主原创文章,转载请标明出处. https://blog.csdn.net/u013309870/ar ...

  3. LeetCode探索初级算法 - 动态规划

    LeetCode探索初级算法 - 动态规划 今天在LeetCode上做了几个简单的动态规划的题目,也算是对动态规划有个基本的了解了.现在对动态规划这个算法做一个简单的总结. 什么是动态规划 动态规划英 ...

  4. ASP.NET Core中使用IOC三部曲(二.采用Autofac来替换IOC容器,并实现属性注入)

    前言 本文主要是详解一下在ASP.NET Core中,自带的IOC容器相关的使用方式和注入类型的生命周期. 这里就不详细的赘述IOC是什么 以及DI是什么了.. emm..不懂的可以自行百度. 目录 ...

  5. 算法-动态规划DP小记

    算法-动态规划DP小记 动态规划算法是一种比较灵活的算法,针对具体的问题要具体分析,其宗旨就是要找出要解决问题的状态,然后逆向转化为求解子问题,最终回到已知的初始态,然后再顺序累计各个子问题的解从而得 ...

  6. python基础(9)--递归、二叉算法、多维数组、正则表达式

    1.递归 在函数内部,可以调其他函数,如果一个函数在内部调用它本身,这个函数就是递归函数.递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于裂解 递归算法解决问题的特点: 1)递归是 ...

  7. hihocoder#1098 : 最小生成树二·Kruscal算法

    #1098 : 最小生成树二·Kruscal算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用 ...

  8. Hihocoder #1098 : 最小生成树二·Kruskal算法 ( *【模板】 )

    #1098 : 最小生成树二·Kruscal算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用 ...

  9. 垃圾回收GC:.Net自己主动内存管理 上(二)内存算法

    垃圾回收GC:.Net自己主动内存管理 上(二)内存算法 垃圾回收GC:.Net自己主动内存管理 上(一)内存分配 垃圾回收GC:.Net自己主动内存管理 上(二)内存算法 垃圾回收GC:.Net自己 ...

随机推荐

  1. LR java Vuser 相关依赖JAR包,配置文件处置方法

    JAR包,配置文件依赖有两种处理方法 1.放到工程文件夹下(lr脚本目录),不支持负载机调用 2.F4  classpath设置加载jar包和配置文件的整个文件夹,麻烦些,但支持负载机调用(与http ...

  2. logstash安装与基础用法

    若是搭建elk,建议先安装好elasticsearch 来自官网,版本为2.3 wget -c https://download.elastic.co/logstash/logstash/packag ...

  3. SELinux深入理解

    ps:今天在远程给服务器配置https的时候,一直乱码,以前做系统的系统第一件事情,就是关闭selinx,今天忘记了,然后就悲剧了... 弄了半天才弄好,镇定思痛,好好的来看下selinux 1. 简 ...

  4. hadoop2.0初识1.3

    1.配置分布式hadoop 1.1 准备三台测试机(虚拟机就可以) 1.1.1 将life-hadoop虚拟机克隆2个分别为life-hadoop02和life-hadoop03 1.1.2 查看ip ...

  5. jquery之empty()方法详解

    empty()函数用于清空每个匹配元素内的所有内容. empty()函数将会移除每个匹配元素的所有子节点(包括文本节点.注释节点等所有类型的节点). 该函数属于jQuery对象(实例). 语法 jQu ...

  6. Determining Current Block and Current Item in Oracle Forms

    SYSTEM.CURSOR_BLOCK Determining current block in Oracle Forms Using SYSTEM.CURSOR_BLOCK system varia ...

  7. hdu 4358 Boring counting dfs序+莫队+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  8. win7文件夹图标中多了一把小锁打不开文件夹怎么办?

    win7文件夹图标中多了一把小锁打不开文件夹怎么办?解决办法一:右击目录→取得管理员权限!该方法适用于win7旗舰版.解决办法二:右击目录→属性→安全→高级→选择everyone→更改权限→勾上完全访 ...

  9. UNIX-LINUX编程实践教程->第八章->实例代码注解->写一个简单的shell

    一 分析 要实现一个shell,需包含3个步骤 1)读入指令 2)指令解析 3)执行指令 1 从键盘读入指令 从键盘读入指令的几个要点: 1)调用getc函数等待并获取用户键盘输入. 2)每一行命令的 ...

  10. dos2unix,去掉Linux下文件中的^M

    Windows系统下使用VS2010编写好的CPP文件,想放到Linux上进行编译.发现Linux上文件中的每行代码末尾都跟着^M这个符号. 为什么同一份文件在windows上和Linux上显示的不一 ...