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] = ...
随机推荐
- mybatis 基础(一) xml配置
如果文章有误,请各位楼下评论,感谢各位积极修正! 一起学习,成为大佬! mybatis: 1.轻量级 2.高级映射(实体类与数据库表字段的映射) 这样就可以后续开发中去操作实体类而不需要去关注数据库, ...
- acm java入门(转载)
ACM中java的使用 http://www.cnblogs.com/XBWer/archive/2012/06/24/2560532.html 这里指的java速成,只限于java语法,包括输入输出 ...
- django初步了解
目录 学前了解 wsgiref模块( web服务网关接口) 根据不同的功能拆封成不同的py文件 动静态网页 HTTP协议 django初步了解1 1.小白必会三板斧 2.静态文件配置 3.reques ...
- Linux7_MySQL5.7_主从复制_scripts
# cat my_full_backup.sh #!/bin/bash BEGINTIME=`date +"%Y-%m-%d %H:%M:%S"` format_time=`dat ...
- 关于HA(2.102 -2.103 服务器排障)
关于处理RHCA故障的报告: ,2.102 和 2.103 两台机器在重启之后拉不起来 原因是这两台服务比较怪 先要启动service rpcbind restart 然后再要起service nfs ...
- js小功能2:切换
HTML: <div id="tb"> <ul><li class="on">房产</li><li> ...
- windows环境下,kafka常用命令
创建topics kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partition 3 - ...
- springboot搭建web项目与使用配置文件
目录 一.准备工作 二.创建基础web项目 1. maven配置 2.创建maven项目.配置pom.xml为web基础项目 3.编写启动类 4.使用maven打包 5.使用命令java -jar x ...
- MySQL连表查询练习题
1.建库 库名:linux50 字符集:utf8 校验规则:utf8_general_ci  create database linux4 charset utf8 default collate ...
- (六)buildroot使用详解
为什么要使用buildroot? (文件系统搭建,强烈建议直接用buildroot,官网[http://buildroot.uclibc.org/]上有使用教程非常详细)文件系统通常要包含很多第三方软 ...