F. String Compression

利用dp和前缀数组来写

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

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

代码:

  1. // Created by CAD on 2019/8/9.
  2. #include <bits/stdc++.h>
  3. #define ll long long
  4. #define fi first
  5. #define se second
  6. #define inf 0x3f3f3f3f
  7. #define INF 0x3f3f3f3f3f
  8. #define PII pair<int,int>
  9. #define PIII pair<pair<int,int>,int>
  10. #define mst(name, value) memset(name,value,sizeof(name))
  11. #define FOPEN freopen("C:\\Users\\14016\\Desktop\\cad.txt","r",stdin)
  12. #define test(n) cout<<n<<endl
  13. using namespace std;
  14. const int maxn=8005;
  15. int Next[maxn];
  16. void getNext(string t)
  17. {
  18. int tlen=t.length(),i=0,j=-1;
  19. Next[0]=-1;
  20. while(i<tlen)
  21. {
  22. if(j==-1||t[i]==t[j])
  23. Next[++i]=++j;
  24. else j=Next[j];
  25. }
  26. }
  27. int getdigit(int n)
  28. {
  29. int cnt=0;
  30. while(n>0)
  31. cnt++,n/=10;
  32. return cnt;
  33. }
  34. int dp[maxn],bj[maxn][maxn];
  35. int main()
  36. {
  37. ios::sync_with_stdio(false);
  38. string s;
  39. cin>>s;
  40. int slen=s.length();
  41. for(int i=0;i<slen;++i)
  42. {
  43. bj[i][i]=2;
  44. int len=slen-i;
  45. string t=s.substr(i,len);
  46. getNext(t);
  47. for(int j=i+1;j<slen;++j)
  48. {
  49. int k=j-i+1;
  50. int p=k-Next[k];
  51. if(k%p==0)
  52. bj[i][j]=getdigit(k/p)+p;
  53. else bj[i][j]=j-i+2;
  54. }
  55. }
  56. dp[0]=0;
  57. for(int i=1;i<=slen;++i)
  58. {
  59. dp[i]=i+1;
  60. for(int j=0;j<i;++j)
  61. dp[i]=min(dp[i],dp[j]+bj[j][i-1]);
  62. }
  63. cout<<dp[slen]<<endl;
  64. return 0;
  65. }

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. Mysql workbench 字段类型(转载)

    转载自:https://blog.csdn.net/j_h_xie/article/details/52924521 项目初始,在使用workbench建表时,字段中有PK,NN,UQ,BIN,UN, ...

  2. springboot2.X版本得@Transactional注解事务不回滚不起作用

    参考文章  https://my.oschina.net/happyBKs/blog/1624482   https://blog.csdn.net/u011410529/article/detail ...

  3. eclipse运行jsp出现404错误怎么办?

    Window/Show View/Other/Server/Servers/双击“Tomcat v7.0 Server at localhost”在Server Locations配置中选择第二个选项 ...

  4. 在web项目中配置log4j

    在web.xml中添加如下代码 <context-param> <param-name>contextConfigLocation</param-name> < ...

  5. sql server delete语句

    delete语句 --DELETE 语句用于删除表中的行 语法:delete from 表名称 where 列名称 = 值 --可以在不删除表的情况下删除所有的行.这意味着表的结构.属性和索引都是完整 ...

  6. 第五章 表表达式 T-SQL语言基础

    表表达式 Microsoft SQL Server支持4种类型的表表达式:派生表(derived table),公用表表达式(CTE,common table expression),视图,以及内联表 ...

  7. 关于不同retina的布局

    不同retina,显示高度不一样,很显然最细的线条最美 <!DOCTYPE html> <html lang="en"> <head> < ...

  8. 【有钱的大佬看过来】Java开发学习大纲

    Java开发学习大纲文档V7.0 有钱的大佬可以买下这个版权,全网最完整最详细了,没钱的大佬可以按照自己的方式去整理.有需要的私聊作者QQ:253173641 来源于-幸福的沉淀:https://ww ...

  9. kubernetes创建资源的两种方式

    一.创建方式分类: 命令 vs 配置文件 Kubernetes 支持两种方式创建资源: 1.用 kubectl 命令行的方式直接创建,比如: kubectl run httpd-app --image ...

  10. opengl学习-利用模板测试勾画物体轮廓中出现的一个问题

    我在学习OpenGL模板测试勾画物体轮廓的时候,出现了这个问题: 这个出现的原因就是,改变摄像机的时候,每次绘制,上次绘制中模板缓冲区的数据没有清除的原因.也就是在while循环开始的时候,glCle ...