Codeforces 825F - String Compression
825F - String Compression
题意
给出一个字符串,你要把它尽量压缩成一个短的字符串,比如一个字符串ababab你可以转化成3ab,长度为 3,比如bbbacacb转化成3b2ac1b,长度为 7,aaaaaaaaaa转化为10a,长度为 3。
分析
求转换后的最短字符串,那么怎么去组合字符串中的子串是关键。
考虑 dp,dp[1...L] 对应字符串 S[0...L-1] 。dp[i] 表示字符串 S[0, i - 1] 转化后的字符串长度,dp[0] = 0。
状态转移方程:$ dp[i] = min(dp[i], dp[j] + has[j][i - 1]) $,其中 \(has[j][i - 1]\) 表示字符串 S[j, i - 1] 由某个字符(串)重复 k 次,k 取最大的时候,转化后的长度。比如abab,ab重复了两次,那么 \(has\) 的值为 3。
那么我们就要去预处理 has 的值,一个字符串(长度为 L)由 k 个连续且相同的子串组成,求 k 的最大值。这个问题不就是某个经典的字符串题吗?戳
当然不用后缀数组那么麻烦,那只是为了练习 : ) 。
考虑 KMP 算法中 nxt 数组的含义,对于字符串abcabc,那么 \(nxt[6] = 3\),即前缀后缀相同的子串长,我们称最小连续的那个子串为循环节,设 \(l = L - nxt[L]\),如果 \(l\) 刚好整除 \(L\),那么 \(l\) 就是循环节的长度。对于字符串ababab, \(nxt[6]=4\) ,假如后面还有字符,第 7 个字符导致失配,最后的四个字符abab不用再去匹配了,直接由abab向后匹配,正好是右移了一个循环节的位置。
code
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 8000 + 10;
const int INF = 1e9;
char T[MAXN];
int nxt[MAXN];
void getNext() {
int tlen = strlen(T);
int j, k;
j = 0;
k = -1;
nxt[0] = -1;
while(j < tlen)
if(k == -1 || T[j] == T[k])
nxt[++j] = ++k;
else
k = nxt[k];
}
int getl(int x) {
int cnt = 0;
while(x) {
cnt++;
x /= 10;
}
return cnt;
}
char S[MAXN];
int dp[MAXN];
int has[MAXN][MAXN];
int main() {
scanf("%s", S);
int L = strlen(S);
for(int i = 0; i < L; i++) {
has[i][i] = 2;
int l = 0;
for(int j = i; j < L; j++) {
T[l++] = S[j];
}
T[l] = 0;
getNext();
l = 0;
for(int j = i + 1; j < L; j++) {
l++;
int k = j - i + 1;
int p = k - nxt[l + 1];
if(k % p == 0) {
has[i][j] = getl(k / p) + p;
} else has[i][j] = j - i + 2;
}
}
dp[0] = 0; // dp[1..L] 对应字符串 S[0..L-1]
for(int i = 1; i <= L; i++) {
dp[i] = i + 1;
for(int j = 0; j < i; j++) {
// [j + 1, i] 对应字符串的 [j, i - 1]
dp[i] = min(dp[i], dp[j] + has[j][i - 1]);
}
}
printf("%d\n", dp[L]);
return 0;
}
Codeforces 825F - String Compression的更多相关文章
- Codeforces 852F String Compression
题目 OvO http://codeforces.com/contest/825/problem/F 题解 KMP+DP 十分优雅地利用了KMP的fail数组 fail[k]表示第k个后缀的的fail ...
- 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及selenium
安装pyhton 访问Python官网:http://www.python.org 下载页Windows下找到适合64位系统的版本 下载后双击安装 安装后查看计算机->属性->高级系统设置 ...
- paramiko类Fabric主机管理
环境:Linux python3.5 要求:类 Fabric 主机管理程序开发:1. 运行程序列出主机组或者主机列表2. 选择指定主机或主机组3. 选择让主机或者主机组执行命令或者向其传输文件(上传/ ...
- codebolocks 中文使用手册1.1
Code::Blocks手册 使用篇 中文翻译版- 原手册下载:http://www.codeblocks.org/docs/manual_en.pdf 译者:JGood 译者言:工欲善其事,必先利其 ...
- Linux 网卡特性配置ethtool详解
近期遇到一个自定义报文传输性能问题,解决过程中借助了ethtool这个工具,因此发掘一下与此工具相关的网卡的一些特性. ethtool 常用命令如下,比如对eth0的操作: ethtool eth0 ...
- python基础--用python执行系统命令
from os import system print(system('ping www.baidu.com'))
- CentOS7 设置开机直接进入命令行界面
上网查询centsos设置开机直接进入命令行界面的方法都说修改/etc/inittab文件,将文件中的“ :id:5:initdefault:”改为“ :id:3:initdefault:”,即将默认 ...
- [洛谷P3805]【模板】manacher算法
题目大意:给你一个字符串,求出它的最长回文字段 题解:$manacher$算法 卡点:$p$数组未开两倍空间 C++ Code: #include <cstdio> #include &l ...
- linux正则表达式(一)
正则表达式是一种字符串模式匹配,使用灵活.功能强大,使用简单的方式对字符串进行控制. 1.使用grep进行字符串匹配 测试文本 1.txt helloworld haa !@#$%^&*( A ...
- Tomcat学习笔记(五)
生命周期事件 Catalina包含有很多组件.当Catalina启动时,这些组件也会启动,同样,当Catalina关闭时,这些组件也随之关闭,通过实现org.apache.catalina.Lifec ...
- [03] html 中引入与使用css
1. 使用style属性 <a style="color: red;"> hello ,there use style attribute</a> 2. l ...