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] = ...
随机推荐
- MySQL中的数据类型 [数值型、字符串型、时间日期型]
MySQL中的数据类型 [数值型.字符串型.时间日期型] MySQL中各数据类型 1. 数值类型(整型) 类型 数据大小 类型 (无符号:unsigned) 数据大小 存储空间 tinyint -12 ...
- mysql5.6 Centos6.6安装
1.检查防火墙 是否关闭service iptables status service iptables stopchkconfig iptables off 2. SELINUXvim /etc/s ...
- static char定义的用法
static char *p是全局静态变量,char *p是临时变量,static定义的你在其他地方可以调用,而且是通用的,也就是说你在一个地方改了它的值,其他地方也就跟着改了,而char *p只是一 ...
- iptables笔记
一.内核转发 *永久开启转发 sysctl -w net.ipv4.ip_forward=1 *查看当前 cat /proc/sys/net/ipv4/ip_forward * 暂时开启 echo 1 ...
- 协议相关(HTTP,TCP,webservice,socket)
什么是协议? 我们常常点开的链接(URL)就有HTTP.HTTPS协议 枯燥点的知识(协议模型) HTTP,webservice都是在<TCP/IP协议>的应用层. TCP,socket在 ...
- 快速上手小程序的mpvue框架
一.什么是mpvue框架? mpvue 是一个使用 Vue.js 开发小程序的前端框架.框架基于 Vue.js 核心(所以建议熟练掌握vue再使用mpvue框架,否则还是建议去使用原生框架去写小程序) ...
- Datetime 在C#中的用法 获取当前时间的各种格式
DateTime 获得当前系统时间: DateTime dt = DateTime.Now; Environment.TickCount可以得到“系统启动到现在”的毫秒值 DateTime now = ...
- BZOJ4887可乐题解--矩阵运算
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4887 分析 话说这道题经常见到类似模型来计数算期望,概率啊,然而我太蒻了都不会做,今天看 ...
- js跳转页面与打开新窗口的方法
1.超链接<a href="http://www.jb51.net" title="脚本之家">Welcome</a> 等效于js代码 ...
- 【Swift后台】目录
背景介绍 环境安装