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] = ...
随机推荐
- adb启动和关闭
启动adb服务: cmd("adb start-server"); 关闭adb服务: cmd("adb start-server");
- linux系统下怎么关闭一个端口
netstat -an | grep 22查看22端口 netstat -ntulp |grep 80 //查看所有80端口使用情况
- Python全栈工程师(文件操作、编码)
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 为了梦想为了信仰 Python人工智能从入门到精通 最近简直要死了 发烧感冒 喉咙痛..... ...
- CodeForces-999D Equalize the Remainders
题目链接 https://vjudge.net/problem/CodeForces-999D 题面 Description You are given an array consisting of ...
- 聊聊、Mybatis XML
引入相应的依赖包 <dependency><groupId>org.mybatis</groupId><artifactId>mybatis-sprin ...
- 团队项目-第六次Scrum 会议
时间:11.1 时长:30分钟 地点:F楼2层沙发休息处 工作情况 团队成员 已完成任务 待完成任务 解小锐 完成员工信息的简单初始化 学习cocos creator样例 陈鑫 完成CurrentPr ...
- jquery serialize() 方法
ajax异步提交的时候,会使用该方法. 方法:jQuery ajax - serialize() 方法
- 【Android】实验8 SQLite数据库操作2016.5.12
实验8 SQLite数据库操作 [目的] 设计一个个人通讯录,掌握Android平台下的数据库开发,该个人通讯录主要包括联系人列表和联系人详细信息等界面. [要求] 程序主界面是通讯录的目录显示手机 ...
- 【操作系统】关于C语言设计程序退出自动关闭窗口的问题
有些同学在做实验一 命令解释程序的编写的时候,输入quit命令退出程序,窗口并没有关闭,如下图所示需要Press any key to continue(按任意键)之后才关闭. 出现这个结果的原因是在 ...
- 【距离GDOI:128天】【POJ2778】DNA Sequence(AC自动机+矩阵加速)
已经128天了?怎么觉得上次倒计时150天的日子还很近啊 ....好吧为了把AC自动机搞透我也是蛮拼的..把1030和这道题对比了无数遍...最终结论是...无视时间复杂度,1030可以用这种写法解. ...