F. String Compression

利用dp和前缀数组来写

dp[i] 所表示的东西是 字符串 s[0:i] (不包括 s[i])能够压缩的最短长度

bj[i][j] 表示的是字符串 s[i:j+1] (不包括 s[j+1])能够压缩的最短长度

代码:

// Created by CAD on 2019/8/9.
#include <bits/stdc++.h> #define ll long long
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f
#define PII pair<int,int>
#define PIII pair<pair<int,int>,int>
#define mst(name, value) memset(name,value,sizeof(name))
#define FOPEN freopen("C:\\Users\\14016\\Desktop\\cad.txt","r",stdin)
#define test(n) cout<<n<<endl
using namespace std; const int maxn=8005;
int Next[maxn];
void getNext(string t)
{
int tlen=t.length(),i=0,j=-1;
Next[0]=-1;
while(i<tlen)
{
if(j==-1||t[i]==t[j])
Next[++i]=++j;
else j=Next[j];
}
}
int getdigit(int n)
{
int cnt=0;
while(n>0)
cnt++,n/=10;
return cnt;
}
int dp[maxn],bj[maxn][maxn];
int main()
{
ios::sync_with_stdio(false);
string s;
cin>>s;
int slen=s.length();
for(int i=0;i<slen;++i)
{
bj[i][i]=2;
int len=slen-i;
string t=s.substr(i,len);
getNext(t);
for(int j=i+1;j<slen;++j)
{
int k=j-i+1;
int p=k-Next[k];
if(k%p==0)
bj[i][j]=getdigit(k/p)+p;
else bj[i][j]=j-i+2;
}
}
dp[0]=0;
for(int i=1;i<=slen;++i)
{
dp[i]=i+1;
for(int j=0;j<i;++j)
dp[i]=min(dp[i],dp[j]+bj[j][i-1]);
}
cout<<dp[slen]<<endl;
return 0;
}

String Compression的更多相关文章

  1. UVA 1351 十三 String Compression

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

  2. 【leetcode】443. String Compression

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

  3. 443. String Compression

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

  4. CF825F String Compression 解题报告

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

  5. 213. String Compression【LintCode java】

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

  6. 213. String Compression【easy】

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

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

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

  8. Codeforces 825F - String Compression

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

  9. 区间DP UVA 1351 String Compression

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

  10. LeetCode_443. String Compression

    443. String Compression Easy Given an array of characters, compress it in-place. The length after co ...

随机推荐

  1. CSP-J 2019 T3 纪念品

    \(\mathfrak{a}\).反思: 通过这道题成功发现自己的背包还是很差\(w\): 可能这是我\(gu\)了好久好久博客的报应叭 就在做这个题的时候,自己连背包\(dp\)的思想都忘了 背包可 ...

  2. AcWing登山

    这是2006北大举办的ACM的一道题. 题意为:给定景点海拔高度,队员们不去游览相同高度的景点,一开始往上爬,一但往下爬就不能再向上爬,求最多可以游览多少个景点.那么我们可以得到一个结论:以一个最高点 ...

  3. # 数字签名&数字证书

    目录 数字签名&数字证书 数字签名 数字证书 数字证书的实例(https协议) 数字签名&数字证书 参考资料: 数字签名是什么?-阮一峰的网络日志 数字签名和数字证书究竟是什么?知乎- ...

  4. mybatis字符#与字符$的区别

    问题:使用in查询查询出一批数据,in查询的参数是字符串拼接的.调试过程中,把mybatis输出的sql复制到navicat中,在控制台将sql的参数也复制出来,替换到sql的字符 '?' 的位置,执 ...

  5. iOS响应链和传递机制

    iOS中加载的时候会先执行main函数 int main(int argc, charchar * argv[]) { @autoreleasepool { return UIApplicationM ...

  6. Linux使用Docker启动Elasticsearch并配合Kibana使用,安装ik分词器

    注意事项 这里我的Linux虚拟机的IP地址是192.168.1.3 Docker运行Elasticsearch容器之后不会立即有反应,要等一会,等待容器内部启动Elasticsearch,才可以访问 ...

  7. Tensorflow模型移植Arm之一:C与Python互相调用

    一.C调用Python 1.新建一个Python文件,名称为py_multipy.py: #import numpy as np def multiply(a=1,b=2): print('Funct ...

  8. 简单了解TCP/IP与HTTP

    这种东西网上资源非常丰富,完全没有必要造轮子,更何况 这也不是你能造的啊,来 上连接 TCP/IP https://www.runoob.com/tcpip/tcpip-intro.html HTTP ...

  9. 【bzoj 4025 改编版】graph

    题意 给定一张 \(n\) 个点 \(m\) 条边的无向图,问删去每个点后,原图是不是二分图.输出一个长度为 \(n\) 的 \(\text{01}\) 串表示答案. 多组数据. \(T\le 5,\ ...

  10. ZOJ red black tree

    #include <bits/stdc++.h> #define fi first #define se second #define lson l,m,rt<<1 #defi ...