Description

Lamps-O-Matic company assembles very large chandeliers. A chandelier consists of multiple levels. On the first level crystal pendants are attached to the rings. Assembled rings and new pendants are attached to the rings of the next level, and so on. At the end there is a single large ring -- the complete chandelier with multiple smaller rings and pendants hanging from it.  A special-purpose robot assembles chandeliers. It has a supply of crystal pendants and empty rings, and a stack to store elements of a chandelier during assembly. Initially the stack is empty. Robot executes a list of commands to assemble a chandelier. On command "a" robot takes a new crystal pendant and places it on the top of the stack. On command "1" to "9" robot takes the corresponding number of items from the top of the stack and consecutively attaches them to the new ring. The newly assembled ring is then placed on the top of the stack. At the end of the program there is a single item on the stack -- the complete chandelier.  Unfortunately, for some programs it turns out that the stack during their execution needs to store too many items at some moments. Your task is to optimize the given program, so that the overall design of the respective chandelier remains the same, but the maximal number of items on the stack during the execution is minimal. A pendant or any complex multi-level assembled ring count as a single item of the stack.  The design of a chandelier is considered to be the same if each ring contains the same items in the same order. Since rings are circular it does not matter what item is on the top of the stack when the robot receives a command to assemble a new ring, but the relative order of the items on the stack is important. For example, if the robot receives command "4" when items < i1, i2, i3, i4 > are on the top of the stack in this order (i1 being the topmost), then the same ring is also assembled if these items are arranged on the stack in the following ways: < i2, i3, i4, i1 >, or < i3, i4, i1, i2 >, or < i4, i1, i2, i3 >.

Input

The input contains a single line with a valid program for the robot. The program consists of at most 10 000 characters.

Output

On the first line of the output file write the minimal required stack capacity (number of items it can hold) to assemble the chandelier. On the second line write some program for the assembly robot that uses stack of this capacity and results in the same chandelier.

题目大意:太难说了不写了。

思路: 大概就是递推处理对每个数字组合起来所需要的最小栈吧……思路挺难搞的我已经不会描述了……


http://hi.baidu.com/billdu/item/50dd9fb49364269619469705

方法就是每到一个数字命令,就枚举前面的元素怎样排列。由于保证了元素数目不多于9,所以圆排列只用枚举9个,时间上绰绰有余。

枚举一个情况下的计算是重点。设定任意一个元素【操作时要占用的最大堆栈数】为m,比如说,组装这个元素以前栈中有5个元素,中间的某一步栈中有12个元素,并且自始至终没超过12个,那么该元素的的M = 7。单个元素的M为1。(这很容易理解……)

然后设某数字指令要拼装的的元素集合为A,元素的安装位置设为p的话(头一个元素是第0个,之后是第1个,依此类推),在这种枚举的情况下目标元素的m = max{ p(E) + m(E), E∈A },因为对于每一个元素,在这之前已经安装了p(E)个元素,安装本元素需要再开m(E)的空间。枚举所有情况,找出最小的目标m记录下来,直到最后最终的元素的m值就是所要用的栈。

输出方案很简单,只需要递归输出,注意顺序既可。


 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cctype>
using namespace std; const int MAXN = ; char s[MAXN];
int src[MAXN], n; void init() {
int i;
for(i = ; s[i]; ++i)
src[i] = isdigit(s[i]) * (s[i] - '');
n = i;
} int best[MAXN];
int ans[MAXN][];
int pos[];
int stk[MAXN], top; void next(int *arr, int n) {
for(int i = ; i < n; ++i)
if(++arr[i] >= n) arr[i] = ;
} void solve(int n) {
if(src[n] == ) {
best[n] = ;
stk[top++] = n;
return ;
}
int &len = src[n];
for(int i = ; i < len; ++i) pos[i] = i;
for(int i = ; i < len; ++i) {
int tmp = len;
for(int j = ; j < len; ++j)
tmp = max(tmp, j + best[stk[top - len + pos[j]]]);
if(best[n] == || tmp < best[n]) {
best[n] = tmp;
for(int j = ; j < len; ++j) ans[n][j] = stk[top - len + pos[j]];
}
next(pos, len);
}
top -= len;
stk[top++] = n;
} void dfs(int n) {
for(int i = ; i < src[n]; ++i)
if(src[ans[n][i]]) dfs(ans[n][i]);
else putchar('a');
printf("%d", src[n]);
} int main() {
scanf("%s", s);
init();
for(int i = ; i < n; ++i) solve(i);
printf("%d\n", best[n - ]);
dfs(n - );
puts("");
}

POJ 2161 Chandelier(动态规划)的更多相关文章

  1. POJ 2161 Chandelier(树状DP)

    一.题意 首先是对题目的翻译.给出一个长长的字符串,这个字符串描述了一个吊灯.对于给字符串只有两种操作数——'a'为一个吊灯灯珠,将改灯珠入栈,一位阿拉伯数字K,代表一个环,将把该数字前面k位数都出栈 ...

  2. poj 3783 Balls 动态规划 100层楼投鸡蛋问题

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098409.html 题目链接:poj 3783 Balls 动态规划 100层楼投鸡蛋问题 ...

  3. poj 2229 一道动态规划思维题

    http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...

  4. [POJ 2063] Investment (动态规划)

    题目链接:http://poj.org/problem?id=2063 题意:银行每年提供d种债券,每种债券需要付出p[i]块钱,然后一年的收入是v[i],到期后我们把本金+收入取出来作为下一年度本金 ...

  5. [POJ 2923] Relocation (动态规划 状态压缩)

    题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...

  6. POJ 1088 滑雪 -- 动态规划

    题目地址:http://poj.org/problem?id=1088 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

  7. poj 1159 Palindrome - 动态规划

    A palindrome is a symmetrical string, that is, a string read identically from left to right as well ...

  8. poj 2385【动态规划】

    poj 2385 Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14007   Accepte ...

  9. poj 1837 Balance 动态规划 (经典好题,很锻炼思维)

    题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...

随机推荐

  1. linux c做服务端使用多线程接收图片并且将图片写入数据库

    #include<sys/socket.h> #include<sys/types.h> #include<sys/stat.h>//包含文件的全部结构,属性 #i ...

  2. Spring知识点总结(四)之SpringAOP基础

        1. Spring aop中的基本概念        • 连接点(Joinpoint):在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候.在Spring AOP中,一个连接 ...

  3. python使用sqlalchemy连接mysql数据库

    环境:centos7+python2.7.5+sqlalchemy sqlalchemy是python当中比较出名的orm程序.在python中,使用sqlalchemy连接mysql数据库进行操作非 ...

  4. 【C语言】素数判定

    题目:素数判定. 编写函数,参数是一个正整数n,如果它是素数,返回1,否则返回0. 分析 质数概念: 质数:除了1之外,只能被它本身整除的正数称为质数 如果这个数能被其他正数整除,说明这个数有两个或以 ...

  5. 简单使用PuTTy登录centos虚拟机

    博主刚刚开始学习Linux,想通过写博客的方式加深对linux学习的记忆 使用虚拟机安装精简版的linux后发现窗口字体太小,于是就想着通过PuTTy登录的方式解决 简单安装linux后 将linux ...

  6. kali2.0 设置输入法 找了好久,亲测有效

    kali2.0更新源启用中文输入法 查看版本信(Version): uname -r uname -r 工具(Tools): fcitx fcitx fcitx-table-wbpy 更新源:(Sou ...

  7. php判断某个数是素数的3种方法

    什么是素数? 质数又称素数.一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数:否则称为合数.(注:1不是素数.) 方法一: 基本方法,——计数方法. $num = 7; $n = ...

  8. 编程 - 前端 - JavaScript - 库 - ECharts (开源可视化)

    ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等) ...

  9. loushang框架的开发中关于BSP的使用,将写好的功能模块部署到主页界面结构上

    前言: 当我们已经开发好相应的模块或者功能的时候,需要将这个功能部署在index主页上作为可点击直接使用的模块,而不是每次需要去浏览对应的url地址. 这时候就需要运用到L5的BSP. 作为刚刚入门l ...

  10. PyCharm+QT Designer整合

    CMD下使用pip安装PyQt4或者PYQT5 这里要注意,你下载的PYQT5不包含QT designer 还要:pip3 install PyQt5-tools,好像Pyqt5中将designer分 ...