题目

  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));

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <map>
  7.  
  8. using namespace std;
  9.  
  10. typedef long long ll;
  11. typedef unsigned long long ull;
  12.  
  13. const int M=;
  14.  
  15. int lv[M];
  16. int fail[M][M];
  17. char s[M];
  18. int ls;
  19. int dp[M];
  20.  
  21. int getLv(int spl)
  22. {
  23. int ret=;
  24. while(spl)
  25. {
  26. spl/=;
  27. ret++;
  28. }
  29. return ret;
  30. }
  31.  
  32. void init()
  33. {
  34. int i,j,k;
  35. for(i=;i<=M;i++)
  36. lv[i]=getLv(i);
  37. for(k=;k<ls;k++)
  38. {
  39. j=-; fail[k][]=-;
  40. for(i=;k+i<=ls;i++)
  41. {
  42. while(j>= && s[k+i-]!=s[k+j])
  43. j=fail[k][j];
  44. j++;
  45. fail[k][i]=j;
  46. }
  47. }
  48. // for(i=0;i<=ls;i++)
  49. // cout<<fail[0][i]<<' ';
  50. // cout<<endl;
  51. }
  52.  
  53. int calcu(int st,int len)
  54. {
  55. if(len== && s[st]==s[st+])
  56. return ;
  57. int ret,tmp;
  58. tmp=fail[st][len];
  59. // cout<<st<<' '<<len<<' '<<tmp<<endl;
  60. if(tmp<(len+)/ || len%(len-tmp)!=)
  61. ret=+len;
  62. else
  63. ret=lv[len/(len-tmp)]+(len-tmp);
  64. return ret;
  65. }
  66.  
  67. void solve()
  68. {
  69. int i,j;
  70. for(i=;i<ls;i++)
  71. {
  72. dp[i]=min(i++,calcu(,i+));
  73. for(j=;j<i;j++)
  74. dp[i]=min(dp[i],dp[j]+calcu(j+,i-(j+)+));
  75. }
  76. cout<<dp[ls-]<<endl;
  77. }
  78.  
  79. int main()
  80. {
  81. int i,j;
  82. cin>>s;
  83. ls=strlen(s);
  84. init();
  85. solve();
  86. return ;
  87. }

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. C# 不用添加WebService引用,调用WebService方法

    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行. [System.Web.Script.Services.ScriptService] 使用HttpWeb ...

  2. MapReduces计数实验

    实验内容 现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1. buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数 ...

  3. Resource Model

    API不应该直接返回Entity,应该是返回一个Resource,不想把entity内部的细节暴漏给外部 viewModel是在MVC中的叫法 使用AutoMapper来对Entity和Resourc ...

  4. 【原创】大数据基础之Kudu(5)kudu增加或删除目录/数据盘

    kudu加减数据盘不能直接修改配置fs_data_dirs后重启,否则会报错: Check failed: _s.ok() Bad status: Already present: FS layout ...

  5. gcc/g++ 以及makefile

    生成可执行文件   g++ mutiprocess.cpp -o test -fpic:产生位置无关码,位置无关码就是可以在进程的任意内存位置执行的目标码,动态链接库必须使用 -c : 只生成 .o ...

  6. 在Markdown中写公式块

    Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. Markdown中的公式语法是遵循LaTex语法的 $ sum = \sum_{i ...

  7. js中with的作用

    js中with的作用当一个对象有多个需要操作的属性或方法时,可以使用如<体>试验<script type=“text/javascript”>var o=文件.创建元素(“DI ...

  8. 如何使用koa搭建一个简单服务

    1.首先检测是否已经有node环境?   把Windows的黑窗体的命令行工具调用出来   敲击命令行node -v , 然后,就可以看到这个打印出了一个版本号,这就证明我们的node.js已经是安装 ...

  9. XSS防御和绕过2

    上一篇已经总结过,这里转载一篇,备忘 0x01 常规插入及其绕过 转自https://blog.csdn.net/qq_29277155/article/details/51320064 1 Scri ...

  10. textarea回填数据显示自适应高度

    queryTextArea(){ var textAll = document.getElementById('templaInner').querySelectorAll("textare ...