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] = ...
随机推荐
- 使用 WijmoJS 轻松实现撤消重做(Undo /Redo)
使用 WijmoJS 轻松实现撤消重做(Undo /Redo) 在V2019.0 Update2 的全新版本中,WijmoJS能够轻松实现撤消和重做操作,使Web应用程序的使用更加友好.更加高效. 不 ...
- Python time strptime()方法
Python time strptime()方法 描述 Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组. 语法 strptime()方法语法: time ...
- 【记录】看见的一些很好的博客x存一下
[字符串] AC自动机:https://www.cnblogs.com/cjyyb/p/7196308.html
- 剑指offer7: 斐波那契数列第n项(从0开始,第0项为0)
1. 题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0).n<=39 2. 思路和方法 斐波那契数列(Fibonacci sequen ...
- php 取post数据的三种方式
$_POST.$GLOBALS['HTTP_RAW_POST_DATA'].file_get_contents("php://input") 都有用来取post数据,用下来感觉大致 ...
- Phython-守护线程
import threading,time def run(n): print("task is ",n) time.sleep(2) print("task done& ...
- Codeforces 1244F. Chips
传送门 显然可以注意到连续的两个相同颜色的位置颜色是不会改变的,并且它还会把自己的颜色每秒往外扩展一个位置 同时对于 $BWBWBW...$ 这样的序列,它每个位置的颜色每一秒变化一次 然后可以发现, ...
- 题解 UVA1316 【Supermarket】
题目链接: https://www.luogu.org/problemnew/show/UVA1316 思路: 根据题目意思,我们需要用到贪心的思想,越晚过期的商品当然是越晚卖好.同时你假如有多个商品 ...
- opencv ORB各参数的含义
ORB中有很多参数可以设置,在OpenCV中它可以通过ORB来创建一个ORB检测器. ORB::ORB(int nfeatures=500, float scaleFactor=1.2f, int n ...
- java实现spark常用算子之Reduce
import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...