题目

  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的更多相关文章

  1. Codeforces 825F - String Compression

    825F - String Compression 题意 给出一个字符串,你要把它尽量压缩成一个短的字符串,比如一个字符串ababab你可以转化成3ab,长度为 3,比如bbbacacb转化成3b2a ...

  2. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

  3. UVA 1351 十三 String Compression

    String Compression Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  4. 【leetcode】443. String Compression

    problem 443. String Compression Input ["a","a","b","b"," ...

  5. 443. String Compression

    原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...

  6. CF825F String Compression 解题报告

    CF825F String Compression 题意 给定一个串s,其中重复出现的子串可以压缩成 "数字+重复的子串" 的形式,数字算长度. 只重复一次的串也要压. 求压缩后的 ...

  7. 213. String Compression【LintCode java】

    Description Implement a method to perform basic string compression using the counts of repeated char ...

  8. 213. String Compression【easy】

    Implement a method to perform basic string compression using the counts of repeated characters. For ...

  9. 区间DP UVA 1351 String Compression

    题目传送门 /* 题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度 区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = ...

随机推荐

  1. JDK1.8 新特性(全)

    原文链接:https://blog.csdn.net/qq_29411737/article/details/80835658

  2. JAVA 中为什么String 是immutable的

    本文翻译自:http://www.programcreek.com/2013/04/why-string-is-immutable-in-java/ 这是一个很老但很流行的问题,这里有几个原因Stri ...

  3. spring-boot war包部署(二)

    环境 jdk 8 tomcat 8.5 sts 4.4.2 maven 3.6.1 背景 有时候,服务器已经有了,我们必须要使用 war 包进行部署,所以需要 spring boot 支持打包和部署成 ...

  4. PAT A1036 Boys vs Girls(25)

    AC代码 #include <cstdio> #include <algorithm> using namespace std; const int max_n = 11000 ...

  5. php用逗号格式化数字

    今日工作需要格式化数字显示当前商品价格,比如2335.32,需要格式化为2,335.32这样显示.我写了一个函数.总感觉这么简单的功能,但是却需要30多行代码来完成. <?php/**** * ...

  6. PHP以table形式导出数据表实现单元格内换行

    <br style='mso-data-placement:same-cell;'>

  7. appium-Android_webview页面元素定位遇到的问题

    如上图所示,该页面包含webview,但是用driver.contexts只获取到了Android原生,而webview的context则没有获取到,所以webview页面的元素.希望有大佬能提供有效 ...

  8. winform messageBox.Show()

    MessageBox.Show(" 5 个参数...... ",     " 亮仔提示",     MessageBoxButtons.OKCancel,    ...

  9. pyquery 库的使用

    from pyquery import PyQuery as pq # 文件勿命名为 pyquery.py,会发生冲突 # 字符串初始化 html = ''' <div id="pag ...

  10. 关于学习电信nb-iot的小结

    关于这几天对nb-iot的学习的总结和遇到的坑 初步学习nb-iot,了解到了nb-iot对于传感器数据传输功能的强大: 废话不多说,对于nb-iot我们选择的有人的模块,选择B5频段也就是电信的nb ...