POJ2176 Folding

描述

给定一个长度不超过100的字符串,求其“压缩”后长度最短的字符串。如有多个,输出任意即可。

其中对于一个字符串\(str\)的“压缩”\(F(str)\)定义如下:

  • 当\(|str|=1\)时,有\(F(str)=str\),\(|F(str)|=1\)。

  • \(F(str_1+str_2)=F(str_1)+F(str_2)\)

  • \(X(str)\)是字符串\(\underbrace{str+str+\cdots+str}_X\)的压缩,且\(|X(str)|=\lfloor\lg X\rfloor+|str|+2\),即包含括号和数字本身。

    如字符串AAAAAAAAAABABABCCD的最短压缩为9(A)3(AB)CCD,长度由\(18\)降为\(12\)。注意2(C)的长度为\(4\),不会比CC更优。

    压缩可以嵌套。如AAAAAAABAAAAAAAABA可以压缩为2(7(A)BA)

思路

设\(F(l,r)\)表示合并字符串\(S_{l\cdots r}\)得到的最短长度。根据定义,字符串的压缩具有叠加性,得到:

\[F(l,r) = \min\limits_{l \leq k < r}\{F(l,k) + F(k,r+1),\text{cost}(l,r)\}
\]

其中\(\text{cost}\)函数表示\(S_{l\cdots r}\)直接被压缩后的长度;若其不能被压缩,则返回\(\infty\)。

子串\((l,r)\)本身就可能被压缩。我们可以考虑从\(1\)至\(\frac{len}{2}\)枚举模式串的长度,如果匹配,则将压缩之后的字符串及其长度作为一个初始决策。之后再枚举合并子区间的价值就可以了。

代码

#include <cstdio>
#include <cstring>
#include <cctype>
using namespace std;
#define rg register
#define openFile(z) freopen(z".in", "r", stdin), freopen(z".out", "w", stdout)
typedef long long ll;
template<class type> inline type quickRead(type sample)
{
type ret = 0, sign = 1; char ch = getchar();
while(! isdigit(ch))
sign = ch == '-' ? -1 : 1, ch = getchar();
while(isdigit(ch))
ret = ret * 10 + ch - '0', ch = getchar();
return sign == -1 ? -ret : ret;
} const int maxLen = 102;
const int Inf = 0x3f3f3f3f;
struct sub_string
{
char str[maxLen];
int len;
}F[maxLen][maxLen];
char template_string[maxLen];
int Len; inline void break_point()
{}//用于debug int main()
{
openFile("POJ2176");
scanf("%s", template_string + 1);
Len = strlen(template_string + 1); for(rg int i = 1; i <= Len; ++ i)
{
F[i][i].str[0] = template_string[i],
F[i][i].len = 1;
}//初始化,单个字符的最短长度只能是1. for(rg int len = 2; len <= Len; ++ len)
{
for(rg int l = 1, r = len ; r <= Len; ++ l, ++ r)
{
F[l][r].len = Inf; for(rg int sub_string_len = 1; sub_string_len <= len >> 1; ++ sub_string_len)
{
//枚举模式串的长度并匹配
if(len % sub_string_len != 0)
continue;//优化:模式串的长度一定是原串的某个约数
int match_left = l, match_right = l + sub_string_len;
while(template_string[match_left] == template_string[match_right] && match_right <= r)
{
++ match_left;
++ match_right;
} if(match_right > r)//匹配成功
{
sprintf(F[l][r].str, "%d", (r - l + 1) / sub_string_len);
strcat(F[l][r].str, "(");
strcat(F[l][r].str, F[l][l + sub_string_len - 1].str);
strcat(F[l][r].str, ")"); F[l][r].len = strlen(F[l][r].str);//将“压缩自己”作为一个候选决策
break;
}
} for(rg int k = l; k < r; ++ k)
{
if(F[l][k].len + F[k + 1][r].len < F[l][r].len)//枚举“合并区间”的决策
{
F[l][r].len = F[l][k].len + F[k + 1][r].len;
strcpy(F[l][r].str, F[l][k].str);
strcat(F[l][r].str, F[k + 1][r].str);
}
}
}
}
puts(F[1][Len].str);
return 0;
}

POJ2176 Folding的更多相关文章

  1. poj2176 Folding【区间DP】

    Folding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1841   Accepted: 642   Special ...

  2. 常规DP专题练习

    POJ2279 Mr. Young's Picture Permutations 题意 Language:Default Mr. Young's Picture Permutations Time L ...

  3. Codeforces Gym 100002 Problem F "Folding" 区间DP

    Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...

  4. eclipse 插件之Code Folding

    功能: eclipse自带折叠包括方法, import, 注释等得折叠功能, code folding 插件对其增强. 1. 下载插件:( 也可以用link方式, 我的是link安装, jar包网上很 ...

  5. Android Folding View(折叠视图、控件)

    版本号:1.0 日期:2014.4.21 版权:© 2014 kince 转载注明出处 非常早之前看过有人求助以下这个效果是怎样实现的,   也就是側滑菜单的一个折叠效果,事实上关于这个效果的实现,谷 ...

  6. Chrome Dev Tools: Code Folding in CSS and Javascript for improved code readiability

    Note : Apply for google chrome canary. You can fold code blocks in CSS (and Sass) and javascript fil ...

  7. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序

    E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...

  8. Eclipse折叠代码 coffee bytes code folding

    提供一个插件下载地址,博客园的: http://files.cnblogs.com/wucg/com.cb.eclipse.folding_1.0.6.jar.zip  将下载的zip文件解压出来的j ...

  9. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding

    地址:http://codeforces.com/contest/765/problem/E 题目: E. Tree Folding time limit per test 2 seconds mem ...

随机推荐

  1. http请求之of_ordering_getmiditem

    //Public function of_ordering_getmiditem (string as_instr,string as_key) returns string //string as_ ...

  2. 浅谈JMX

    JMX在Java编程语言中定义了应用程序以及网络管理和监控的体系结构.设计模式.应用程序接口以及服务.通常使用JMX来监控系统的运行状态或管理系统的某些方面,比如清空缓存.重新加载配置文件等 优点是可 ...

  3. 缓存策略:redis缓存之springCache

    最近通过同学,突然知道服务器的缓存有很多猫腻,这里通过网上查询其他人的资料,进行记录: 缓存策略 比较简单的缓存策略: 1.失效:应用程序先从cache取数据,没有得到,则从数据库中取数据,成功后,放 ...

  4. Eclipse错误提示: Symbol 'xxxx' could not be resolved

    在eclipse中安装maven(网上资源):https://zhinan.sogou.com/guide/detail/?id=1610049267 项目名 右键->configure-> ...

  5. 浅读vuex源码,了解vuex基本原理

    极简版vuex代码 class KVuex { constructor (options) { this.state = options.state this.mutations = options. ...

  6. luogu题解P4198楼房重建--线段树神操作

    题目链接 https://www.luogu.org/problemnew/show/P4198 分析 一句话题意,一条数轴上有若干楼房,坐标为\(xi\)的楼房有高度\(hi\),那么它的斜率为\( ...

  7. maven:无效的目标发行版:11

    maven:无效的目标发行版:11 我之前在博客里是不记录bug和error的处理的,昨天听了一个资深程序员的视频,决定要改习惯了,记录一些自己平时遇到的问题 这个是我在mvn clean insta ...

  8. spring注解定时器

    上一篇文章写了一个在配置文件中设置时间的定时器,现在来写一个注解方式的定时器: 1.工程结构如下: 2.需要执行的代码块: package com.Task; import org.springfra ...

  9. JavaScript的常用浏览器设置

    用什么浏览器?如果您不告诉我您使用的浏览器,我将告诉您有关JavaScript的常用浏览器设置.~火狐在菜单栏中选择工具->选项->内容以查看启用javascript的选项.Interne ...

  10. vuejs 深度监听

    data: { obj: { a: 123 } }, 监听obj中a属性 watch: { 'obj.a': { handler(newName, oldName) { console.log('ob ...