POJ 2176 Folding(区间DP)
题意:给你一个字符串,请把字符串压缩的尽量短,并且输出最短的方案。
例如:AAAAA可压缩为5(A), NEERCYESYESYESNEERCYESYESYES可压缩为2(NEERC3(YES))。
思路:区间DP,设dp[i][j]是把区间[l, r]内的字符压缩之后的最短长度,那么可以想到区间[l, r]可以通过两种方式转换而来:
1 :[i, j]整个区间本来就可以被压缩
2 :由2个子区间合并而来。
第二种转换是区间DP的常见操作,第一种直接暴力枚举可重叠串的长度即可。
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string dp[110][110], s; string _to_string(int num) {
string ans;
while(num) {
ans += num % 10 + '0';
num /= 10;
}
reverse(ans.begin(), ans.end());
return ans;
}
int main() {
int n;
while(cin >> s) {
n = s.length();
for (int i = 0; i < n; i++) {
dp[i][i] = s[i];
}
for (int len = 2; len <= n; len++) {
for (int i = 0; i < n - len + 1; i++) {
int j = i + len - 1;
dp[i][j] = s.substr(i, j - i + 1);
for (int nowl = 1; nowl <= len / 2; nowl++) {
if(len % nowl) continue;
int l = i, r = i + nowl;
while(s[l] == s[r] && r <= j) l++, r++;
if(r > j) {
int num = len / nowl;
dp[i][j] = _to_string(num);
dp[i][j] += "(";
dp[i][j] += dp[i][i + nowl - 1];
dp[i][j] += ")";
//cout<< dp[i][j] <<endl;
break;
}
}
for (int k = i; k < j; k++) {
if(dp[i][j].length() > dp[i][k].length() + dp[k + 1][j].length() || dp[i][j].length() == 0) {
dp[i][j] = dp[i][k] + dp[k + 1][j];
}
}
}
}
cout << dp[0][n - 1] <<endl;
} }
POJ 2176 Folding(区间DP)的更多相关文章
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...
- POJ 1179 - Polygon - [区间DP]
题目链接:http://poj.org/problem?id=1179 Time Limit: 1000MS Memory Limit: 10000K Description Polygon is a ...
- POJ 1160 经典区间dp/四边形优化
链接http://poj.org/problem?id=1160 很好的一个题,涉及到了以前老师说过的一个题目,可惜没往那上面想. 题意,给出N个城镇的地址,他们在一条直线上,现在要选择P个城镇建立邮 ...
- UVA1630 Folding 区间DP
Folding Description Bill is trying to compactly represent sequences of capital alphabetic characte ...
- POJ 1390 Blocks(区间DP)
Blocks [题目链接]Blocks [题目类型]区间DP &题意: 给定n个不同颜色的盒子,连续的相同颜色的k个盒子可以拿走,权值为k*k,求把所有盒子拿完的最大权值 &题解: 这 ...
- poj 2955"Brackets"(区间DP)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56150 Accepted: 19398 Desc ...
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
随机推荐
- HYSBZ - 2038经典莫队算法题
无修改的莫队 emmm莫队的几条性质,必须是离线的,复杂度是n*sqrt(n) 就是通过预处理查询区间,然后从(l,r)转移到(ll,rr),这样的复杂度是曼哈顿距离,即abs(l-ll)+abs(r ...
- spring boot 基础篇 -- 定时任务
在日常项目中,常常会碰到定时监控项目中某个业务的变化,下面是spring boot 集成的定时任务具体配置: @Component public class IndexWarningScheduled ...
- hdu 1907 John(anti nim)
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- android:descendantFocusability用法简析(转)
开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承BaseAdapter,在adapter中按照需求进行编写,问题 ...
- VMware设置NAT网络及 CentOS 7IP配置
1.打开VMware,选择 编辑, 虚拟网络编辑器 2.默认情况下,VMware8为我们NAT所使用的网卡,选中VMnet8 3.此处设置我们的IP地址,这个随便指定,我这里设置成192.168.2 ...
- 四种launchMode
注意:如果在一个singleTop或者singleInstance的ActivityA中通过startActivityForResult()方法来启动另外一个ActivityB,那么系统将直接返回Ac ...
- L124
I have a toothache because there is a cavity in one of my teeth. I founded an orphanage last year an ...
- 真机环境spotlight光源丢失
maya做好的模型生成的fbx,导入到unity之后,pc运行正常,到了ios真机上发现光线丢失,场景内物体都是暗暗的,查出来原因是spot光源丢失了,选中spot光源,在其Render Mode里, ...
- 反编译(javap)操作和可视化界面
1.反编译(javap)操作和可视化界面 演示:
- codevs 2503 失恋28天-缝补礼物
题目描述 Description 话说上回他给女孩送了n件礼物,由于是廉价的所以全部都坏掉了,女孩很在意这些礼物,所以决定自己缝补,但是人生苦短啊,女孩时间有限,她总共有m分钟能去缝补礼物.由于损坏程 ...