poj2176 Folding【区间DP】
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 1841 | Accepted: 642 | Special Judge | ||
Description
- A sequence that contains a single character from 'A' to 'Z' is considered to be a folded sequence. Unfolding of this sequence produces the same sequence of a single character itself.
- If S and Q are folded sequences, then SQ is also a folded sequence. If S unfolds to S' and Q unfolds to Q', then SQ unfolds to S'Q'.
- If S is a folded sequence, then X(S) is also a folded sequence, where X is a decimal representation of an integer number greater than 1. If S unfolds to S', then X(S) unfolds to S' repeated X times.
According to this definition it is easy to unfold any given folded sequence. However, Bill is much more interested in the reverse transformation. He wants to fold the given sequence in such a way that the resulting folded sequence contains the least possible number of characters.
Input
Output
Sample Input
AAAAAAAAAABABABCCD
Sample Output
9(A)3(AB)CCD
Source
题意:
给一个字符串,尽量压缩,让他长度最短。()和数字都是算长度的。所以样例里CC才没有变成2(C)
思路:
能够想到的是子结构是保存区间i,j中最短的串的长度len,以及这个最短的串
状态转移的时候我们有两种操作,一种就是简单的找一个中间的点,把两边的串合并。这个比较简单。
一种是看这个串能如何压缩。于是我们可以去枚举最后压缩了之后的子串的长度,不包括数字和括号。
对于一个区间(i, j)我们从小到大枚举压缩后的子串长度,因为压缩的越小越好。压缩完成后去比较是压缩比较好还是合并比较好。
每一次枚举区间长度和起始点。
//#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map>
#include<set> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; struct seg{
int len;
char str[];
}dp[][];
char s[];
int n; int main(){ while(scanf("%s", s + ) != EOF){
n = strlen(s + );
for(int i = ; i <= n; i++){
dp[i][i].len = ;
dp[i][i].str[] = s[i];
} for(int l = ; l <= n; l++){
for(int i = ; i <= n - l + ; i++){
int j = i + l - ;
dp[i][j].len = inf;
for(int nowl = ; nowl <= l / ; nowl++){//枚举子串压缩后的长度
if(l % nowl)continue;
int st = i, ed = i + nowl;
while(s[st] == s[ed] && ed <= j)st++, ed++;
if(ed > j){
int num = l / nowl;
sprintf(dp[i][j].str, "%d", num);
strcat(dp[i][j].str, "(");
strcat(dp[i][j].str, dp[i][i + nowl - ].str);
strcat(dp[i][j].str, ")");
dp[i][j].len = strlen(dp[i][j].str);
break;
}
}
for(int k = i; k < j; k++){
if(dp[i][j].len > dp[i][k].len + dp[k + ][j].len){
dp[i][j].len = dp[i][k].len + dp[k + ][j].len;
strcpy(dp[i][j].str, dp[i][k].str);
strcat(dp[i][j].str, dp[k + ][j].str);
}
}
}
} printf("%s\n", dp[][n].str);
}
return ;
}
poj2176 Folding【区间DP】的更多相关文章
- Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...
- UVA1630 Folding 区间DP
Folding Description Bill is trying to compactly represent sequences of capital alphabetic characte ...
- UVa 1630 Folding (区间DP)
题意:折叠一个字符串,使得其成为一个尽量短的字符串 例如AAAAAA变成6(A) 而且这个折叠是可以嵌套的,例如 NEEEEERYESYESYESNEEEEERYESYESYES 会变成 2(N5( ...
- POJ 2176 Folding(区间DP)
题意:给你一个字符串,请把字符串压缩的尽量短,并且输出最短的方案. 例如:AAAAA可压缩为5(A), NEERCYESYESYESNEERCYESYESYES可压缩为2(NEERC3(YES)). ...
- POJ2176 Folding
POJ2176 Folding 描述 给定一个长度不超过100的字符串,求其"压缩"后长度最短的字符串.如有多个,输出任意即可. 其中对于一个字符串\(str\)的"压缩 ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
随机推荐
- am335x hid-multitouch.c
am335x在使用电容屏,需要加载hid-multitouch.ko模块 由下面文件生成 kernel/drivers/hid/hid-multitouch.c 内核中编译模块 make module ...
- bcm56150_i2c驱动分析
本文主要关注bsp中,关于smbus(系统管理总线,是i2c的子集)的配置过程,了解如如何配置i2c寄存器.所有发送的数据都会写在FIFO中,使能之后就发送出去.接收数据就从接收寄存器中读取.读取和发 ...
- e652. Getting the Font Faces for a Font Family
To create a Font object to draw text, it is necessary to specify the font face name. This example de ...
- tiny210V2开发板hdmi输出到10.1寸LCD,无图像
tiny210V2开发板hdmi输出到10.1寸LCD,无图像... 用tiny210V2开发板的HDMI接口输出到的10.1寸LCD,LCD无任何现象.说明一下我的情况,我的10.1寸屏LCD是HD ...
- 转载:【原译】Erlang列表处理(Efficiency Guide)
转自:http://www.cnblogs.com/futuredo/archive/2012/10/22/2734186.html List handling 1 Creating a list ...
- Spring-bean的作用域
在大多数情况下,单例bean是很理想的方案.初始化和垃圾回收对象实例所带来的的成本只留给一些小规模任务,在这些任务中,让对象保持无状态并且在应用中反复重用这些对象可能并不合理.在这种情况下,将clas ...
- SharePoint 沙盒无法启动新的解决方案服务的SPUserCodeV4
开发部署时报错: 错误原因:没有启动该服务: 解决方式:打开管理中心—应用程序管理—服务应用程序--管理服务器上的服务,启动该服务即可.
- 【View】之【SimpleWaveView】可多色可刷新的加速球、进度球【demo】
当前版本:SimpleWaveView_v1.0.20140618 先看效果图,这个加速球是动态的,并且当调用了myView.setRefresh(0.8F);方法后可以从当前值动态降到0再升到80% ...
- 感恩节MathType公式编辑器6折优惠启动
MathType是由美国Design Science公司开发功能强大的公式编辑器,专门用来对数学公式的编辑,与常见的文字处理软件和演示程序配合使用,能够在各种文档中加入复杂的数学公式和符号.感恩节期间 ...
- _variant_t和_bstr_t
_variant_t和_bstr_t这两个类分别封装并管理VARIANT和BSTR这两种数据类型, VARIANT和BSTR这两种类型是COM中使用的数据类型. 实现_variant_t向int,St ...