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] = ...
随机推荐
- C# 不用添加WebService引用,调用WebService方法
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行. [System.Web.Script.Services.ScriptService] 使用HttpWeb ...
- MapReduces计数实验
实验内容 现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1. buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数 ...
- Resource Model
API不应该直接返回Entity,应该是返回一个Resource,不想把entity内部的细节暴漏给外部 viewModel是在MVC中的叫法 使用AutoMapper来对Entity和Resourc ...
- 【原创】大数据基础之Kudu(5)kudu增加或删除目录/数据盘
kudu加减数据盘不能直接修改配置fs_data_dirs后重启,否则会报错: Check failed: _s.ok() Bad status: Already present: FS layout ...
- gcc/g++ 以及makefile
生成可执行文件 g++ mutiprocess.cpp -o test -fpic:产生位置无关码,位置无关码就是可以在进程的任意内存位置执行的目标码,动态链接库必须使用 -c : 只生成 .o ...
- 在Markdown中写公式块
Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. Markdown中的公式语法是遵循LaTex语法的 $ sum = \sum_{i ...
- js中with的作用
js中with的作用当一个对象有多个需要操作的属性或方法时,可以使用如<体>试验<script type=“text/javascript”>var o=文件.创建元素(“DI ...
- 如何使用koa搭建一个简单服务
1.首先检测是否已经有node环境? 把Windows的黑窗体的命令行工具调用出来 敲击命令行node -v , 然后,就可以看到这个打印出了一个版本号,这就证明我们的node.js已经是安装 ...
- XSS防御和绕过2
上一篇已经总结过,这里转载一篇,备忘 0x01 常规插入及其绕过 转自https://blog.csdn.net/qq_29277155/article/details/51320064 1 Scri ...
- textarea回填数据显示自适应高度
queryTextArea(){ var textAll = document.getElementById('templaInner').querySelectorAll("textare ...