UVA - 1630 Folding(串折叠)(dp---记忆化搜索)
题意:给出一个由大写字母组成的长度为n(1<=n<=100)的串,“折叠”成一个尽量短的串。折叠可以嵌套。多解时可输出任意解。
分析:
1、dp[l][r]为l~r区间可折叠成的最短串的长度。
2、ans[l][r]为l~r区间可折叠成的最短串。
3、先判断当前研究的串是否能折叠,若不能折叠,再枚举分割线,折叠分隔后可折叠的串,以使处理后的串最短。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;
string s;
string ans[MAXN][MAXN];
int dp[MAXN][MAXN];
int dfs(int l, int r){
if(dp[l][r] != -1) return dp[l][r];
int len = r - l + 1;
if(len == 1){//串的长度为1,不能折叠也不能枚举分割线
ans[l][r] = s[l];
return dp[l][r] = 1;
}
ans[l][r] = s.substr(l, len);
int tmp = len;//以下判断串是否能折叠
for(int i = 1; i <= len / 2; ++i){//枚举循环周期的长度
if(len % i) continue;
bool ok = true;
for(int j = l + i; j <= r; j += i){//判断串是否以周期为i循环
for(int k = 0; k < i; ++k){
if(s[l + k] != s[j + k]){
ok = false;
break;
}
}
if(!ok) break;
}
if(ok){//该串可以按周期为i折叠
char t[10];
sprintf(t, "%d", len / i);//循环串的长度
dfs(l, l + i - 1);//循环串自身可能是可折叠的
string str(t);
str += "(" + ans[l][l + i - 1] + ")";
int nowlen = (int)str.size();
if(nowlen < tmp){//若折叠后的长度小于不折叠,则更新ans[l][r]
tmp = nowlen;
ans[l][r] = str;
}
}
}
if(tmp != len) return dp[l][r] = tmp;//如果可折叠
for(int i = l; i < r; ++i){//该串不可折叠,枚举分割线
int x = dfs(l, i);
int y = dfs(i + 1, r);
if(x + y < tmp){
tmp = x + y;
ans[l][r] = ans[l][i] + ans[i + 1][r];
}
}
return dp[l][r] = tmp;
}
int main(){
while(cin >> s){
memset(dp, -1, sizeof dp);
int len = (int)s.size();
dfs(0, len - 1);
printf("%s\n", ans[0][len - 1].c_str());
}
return 0;
}
UVA - 1630 Folding(串折叠)(dp---记忆化搜索)的更多相关文章
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- UVa 10651 Pebble Solitaire(DP 记忆化搜索)
Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...
- uva 10599 - Robots(II) (dp | 记忆化搜索)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- 状压DP+记忆化搜索 UVA 1252 Twenty Questions
题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...
- ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. Poor Ramzi -dp+记忆化搜索
ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. ...
- 【bzoj5123】[Lydsy12月赛]线段树的匹配 树形dp+记忆化搜索
题目描述 求一棵 $[1,n]$ 的线段树的最大匹配数目与方案数. $n\le 10^{18}$ 题解 树形dp+记忆化搜索 设 $f[l][r]$ 表示根节点为 $[l,r]$ 的线段树,匹配选择根 ...
- 【BZOJ】1415 [Noi2005]聪聪和可可 期望DP+记忆化搜索
[题意]给定无向图,聪聪和可可各自位于一点,可可每单位时间随机向周围走一步或停留,聪聪每单位时间追两步(先走),问追到可可的期望时间.n<=1000. [算法]期望DP+记忆化搜索 [题解]首先 ...
- [题解](树形dp/记忆化搜索)luogu_P1040_加分二叉树
树形dp/记忆化搜索 首先可以看出树形dp,因为第一个问题并不需要知道子树的样子, 然而第二个输出前序遍历,必须知道每个子树的根节点,需要在树形dp过程中记录,递归输出 那么如何求最大加分树——根据中 ...
- poj1664 dp记忆化搜索
http://poj.org/problem?id=1664 Description 把M个相同的苹果放在N个相同的盘子里,同意有的盘子空着不放,问共同拥有多少种不同的分法?(用K表示)5.1.1和1 ...
随机推荐
- 你必须知道的.Net 8.2.2 本质分析
1 .Equals 静态方法 Equals 静态方法实现了对两个对象的相等性判别,其在 System.Object 类型中实现过程可以表 示为: public static bool Equals ...
- centos 禁用ip v6
# sysctl -w net.ipv6.conf.all.disable_ipv6=1 # sysctl -w net.ipv6.conf.default.disable_ipv6=1 # s ...
- /etc/fstab 只读无法修改的解决办法
在做saltstack的时候不小心误把/etc/fstab给注释了 在命令补全的时候开始报错:[root@kafka2 ~]# cat /et-bash: cannot create temp fil ...
- 018、MySQL取满足日期在两个日期之间的所有数据
#查询 SELECT GZJK_CREATEDATE FROM abc_table WHERE ( ( GZJK_CREATEDATE >= UNIX_TIMESTAMP( '2019-08-0 ...
- window安装dlib、face_recognition
face_recognition简介 face_recognition是Python的一个开源人脸识别库,支持Python 3.3+和Python 2.7.引用官网介绍: Recognize and ...
- POJ 1195:Mobile phones 二维树状数组
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16893 Accepted: 7789 De ...
- hibernate保存失败
报错:org.hibernate.jdbc.BdatchedTooManyRowsAffectedException: Batch update returned unexpected row cou ...
- mcu运行时间估算
昨个伙计问我他那个板子的程序运行时间估算问题… 现在说一下估算的思路.首先确定有几个点,板子的主频.时钟周期,机器周期. 首先由主频f得到一个时钟周期为1/f. 再者时钟周期与机器周期有一个比例关系, ...
- SpringBoot#Download
_amaze! 如果不使用fastdfs等分布式的文件存储,有时候还是需要上传文件到web应用所在的服务器的磁盘上,下载文件.下面是一个小demo,关于如何用控制器进行上传和下载. - @PostMa ...
- Mysql 事务隔离级别分析
Mysql默认事务隔离级别是:REPEATABLE-READ --查询当前会话事务隔离级别mysql> select @@tx_isolation; +-----------------+ | ...