CF825F String Compression 解题报告
CF825F String Compression
题意
给定一个串s,其中重复出现的子串可以压缩成 “数字+重复的子串” 的形式,数字算长度。
只重复一次的串也要压。
求压缩后的最小长度。
数据范围
\(0 \le |s| \le 8,000\)
时空范围:
2sec 512mb
时空范围让我们基本可以\(O(N^2)\)做了
先考虑如果原串的每一个子串都求出了它的压缩后长度存在了\(cnt[i][j]\)里,我们就可以很方便的做DP了
令\(dp[i]\)表示长为\(i\)的串的最小压缩程度
\(dp[i]=min_{j=0}^{i-1}(dp[j]+cnt[j+1][i])\)
考虑求出每一个串的压缩后长度
枚举每一个位置为起点的子串,求出此时的\(nxt\)数组
则如果子串的子串中有从头开始循环的串,我们可以用子串的子串的长度\(len-nxt[len]\)得到循环节的长度
如下图
如果长度可以整除循环节的长度,则求出长度。否则直接为原长度+1
Code:
#include <cstdio>
#include <cstring>
int min(int x,int y){return x<y?x:y;}
const int N=8010;
int dp[N],nxt[N],cnt[N][N],n;
char c[N];
int get(int x)
{
int cnt=0;
while(x)
{
cnt++;
x/=10;
}
return cnt;
}
int main()
{
scanf("%s",c+1);
n=strlen(c+1);
memset(dp,0x3f,sizeof(dp));
dp[0]=0;
for(int i=1;i<=n;i++)
{
memset(nxt,0,sizeof(nxt));
cnt[i][i]=2;
nxt[i]=i-1;
for(int j=i+1,k=i-1;j<=n;j++)
{
while(k!=i-1&&c[k+1]!=c[j]) k=nxt[k];
if(c[k+1]==c[j]) k++;
nxt[j]=k;
int len=j+1-i;
if(len%(len-nxt[j]+i-1)==0)
cnt[i][j]=(len-nxt[j]+i-1)+get(len/(len-nxt[j]+i-1));
else
cnt[i][j]=len+1;
}
}
for(int i=1;i<=n;i++)
for(int j=0;j<i;j++)
dp[i]=min(dp[i],dp[j]+cnt[j+1][i]);
printf("%d\n",dp[n]);
return 0;
}
2018.7.25
CF825F String Compression 解题报告的更多相关文章
- 【LeetCode】443. String Compression 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...
- 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ...
- 【LeetCode】604. Design Compressed String Iterator 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护当前字符和次数 日期 题目地址:https://l ...
- 【LeetCode】942. DI String Match 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】541. Reverse String II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 557 Reverse Words in a String III 解题报告
题目要求 Given a string, you need to reverse the order of characters in each word within a sentence whil ...
- LeetCode 942 DI String Match 解题报告
题目要求 Given a string S that only contains "I" (increase) or "D" (decrease), let N ...
随机推荐
- Struts 2(五):输入校验 & 校验框架
第一节 Struts2输入校验 1.1 输入校验的重要性 输入校验分为客户端校验和服务器端校验.客户端校验用来过滤用户的错误操作,一般使用JavaScript代码实现.服务器端校验用来防止非法用户的恶 ...
- python-__getattr__ 和 __getattribute__
python3完全使用了新式类,废弃了旧式类,getattribute作为新式类的一个特性有非常奇妙的作用.查看一些博客和文章后,发现想要彻底理解getattr和getattribute的区别,实际上 ...
- KETTLE监控
kettle单实例环境下自身没有监控工具,但在集群下自带了监控工具. 一.集群自带的监控 kettle自带的集群监控工具可以监控转换的执行情况. 配置好集群后,打开浏览器:输入http://local ...
- lamp一键配置 --转自秋水
https://teddysun.com/lamp LAMP一键安装脚本 最后修改于:2015年11月08日 / 秋水逸冰 / 54,300 次围观 973 本脚本适用环境: 系统支持:CentOS/ ...
- The Bits (思维+找规律)
Description Rudolf is on his way to the castle. Before getting into the castle, the security staff a ...
- 20181023-9 Alpha阶段第2周/共2周 Scrum立会报告+燃尽图 06
作业要求参见: https://edu.cnblogs.com/campus/nenu/2018fall/homework/2289 Scrum master:赵佳璐 一.小组介绍 组长:王一可 组员 ...
- 2014-2015 ACM-ICPC, NEERC, Eastern Subregional Contest Problem H. Pair: normal and paranormal
题目链接:http://codeforces.com/group/aUVPeyEnI2/contest/229669 时间限制:1s 空间限制:64MB 题目大意:给定一个长度为2n,由n个大写字母和 ...
- 使用git下载编译erlang
git clone https://github.com/erlang/otp cd otp git tag git checkout -b OTP- OTP- ./otp_build all exp ...
- "数学口袋精灵"bug
首先要部署这个app项目就是第一步: 一.前提下载并安装JDK 在线图解:手把手教你安装JDK http://www.lvtao.net/server/windows-setup-jdk.h ...
- 用到的C++标准库
std::set<type>, 模板写的平衡二叉树的集合容器, method: insert, count, std:map<int, string>, 映射和多重映射基于某一 ...