Codeforces 852F String Compression
题目
OvO http://codeforces.com/contest/825/problem/F
题解
KMP+DP
十分优雅地利用了KMP的fail数组
fail[k]表示第k个后缀的的fail组数
dp[i]表示到第i个前缀的最优解
由于KMP的fail数组中的fail[i]能用来表达这个字符串的第i个前缀的后缀与这个字符串的前缀的最大匹配长度,所以可以用来在O(1)的时间内计算一个字符串整体作行程编码的最短长度。calcu表达了该过程。
则状态转移方程为 dp[i]=min(dp[i],dp[j]+calcu(j+1,i-(j+1)+1));
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map> using namespace std; typedef long long ll;
typedef unsigned long long ull; const int M=; int lv[M];
int fail[M][M];
char s[M];
int ls;
int dp[M]; int getLv(int spl)
{
int ret=;
while(spl)
{
spl/=;
ret++;
}
return ret;
} void init()
{
int i,j,k;
for(i=;i<=M;i++)
lv[i]=getLv(i);
for(k=;k<ls;k++)
{
j=-; fail[k][]=-;
for(i=;k+i<=ls;i++)
{
while(j>= && s[k+i-]!=s[k+j])
j=fail[k][j];
j++;
fail[k][i]=j;
}
}
// for(i=0;i<=ls;i++)
// cout<<fail[0][i]<<' ';
// cout<<endl;
} int calcu(int st,int len)
{
if(len== && s[st]==s[st+])
return ;
int ret,tmp;
tmp=fail[st][len];
// cout<<st<<' '<<len<<' '<<tmp<<endl;
if(tmp<(len+)/ || len%(len-tmp)!=)
ret=+len;
else
ret=lv[len/(len-tmp)]+(len-tmp);
return ret;
} void solve()
{
int i,j;
for(i=;i<ls;i++)
{
dp[i]=min(i++,calcu(,i+));
for(j=;j<i;j++)
dp[i]=min(dp[i],dp[j]+calcu(j+,i-(j+)+));
}
cout<<dp[ls-]<<endl;
} int main()
{
int i,j;
cin>>s;
ls=strlen(s);
init();
solve();
return ;
}
Codeforces 852F String Compression的更多相关文章
- Codeforces 825F - String Compression
825F - String Compression 题意 给出一个字符串,你要把它尽量压缩成一个短的字符串,比如一个字符串ababab你可以转化成3ab,长度为 3,比如bbbacacb转化成3b2a ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
- UVA 1351 十三 String Compression
String Compression Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
- 443. String Compression
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...
- CF825F String Compression 解题报告
CF825F String Compression 题意 给定一个串s,其中重复出现的子串可以压缩成 "数字+重复的子串" 的形式,数字算长度. 只重复一次的串也要压. 求压缩后的 ...
- 213. String Compression【LintCode java】
Description Implement a method to perform basic string compression using the counts of repeated char ...
- 213. String Compression【easy】
Implement a method to perform basic string compression using the counts of repeated characters. For ...
- 区间DP UVA 1351 String Compression
题目传送门 /* 题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度 区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = ...
随机推荐
- 【Python】【demo实验5】【练习实例】【多个数字组合成不重复三位数】
题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. 程序源代码 ...
- linux系统设置允许密码登录
编辑 /etc/ssh/sshd_config 文件 将PasswordAuthentication 的值改为 yes 然后重启ssh 服务 进入到 /etc/init.d 文件夹内 执行 ./s ...
- DRF 认证 权限 视图 频率
认证组件 使用:写一个认证类,继承BaseAuthentication 在类中写authenticate方法,把request对象传入 能从request对象中取出用户携带的token根据token判 ...
- MySql 枚举和集合 详解
枚举与集合 枚举类型,enum 每个枚举值均有一个索引值: 在列说明中列表值所允许的成员值被从 1 开始编号. 一般来说就是单选,在定义枚举的时候列出所有的可能性: 代码如下 1. create ta ...
- Linux7_MySQL5.7_主从复制_scripts
# cat my_full_backup.sh #!/bin/bash BEGINTIME=`date +"%Y-%m-%d %H:%M:%S"` format_time=`dat ...
- Http请求头缓存设置方法
1.直接在.aspx页面中设置最直接的,在.aspx页面中添加一行如下代码: <%@ OutputCache Duration="3600" VaryByParam=&quo ...
- Lab 色彩模型和取值范围
L∈(0,100) a∈(-128,127) b∈(-128,127) opencv 的Lab数据对齐做了量化,使其处于0-255范围 L=L*2.55 a=a+128 b=b+128
- 使用 “Unicode 字符集 ” 使用错误,应该使用 “使用多字节字符集”
“void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)”: 不能将参数 1 从“const char ...
- canva绘制圆角矩形
在做组态的时候,需要支持矩形圆角格式,但是因为canvas本身不带有圆角矩形,需要自行算出坐标进行绘制 方案一.统一圆角 <!DOCTYPE html> <html> < ...
- 快速写个node命令行工具
1.package.json-bin配置 [创建bat文件,把bat路径添加到PATH中]这些固定的工作可以由npm帮我们完成.package.json中有个bin字段配置: bin: { " ...