题意:给出一个字符串,再给出一个字符串压缩的方法,问该字符串最少能压缩到多少位。

思路:一直没思路啊,最后只能看题解,看了题解发现这题并没有想象中的难……用dp[i][j]表示区间i~j的最小长度,则dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]),另外,还要判断区间i~j的字符串能不能压缩,然后就没有然后了。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=200+10;
char str[maxn];
int dp[maxn][maxn],n;
int check(int s,int t,int k)
{if((t-s+1)%k!=0) return 0;for(int i=s+k;i<=t;i+=k){for(int j=0;j<k;++j)if(str[s+j]!=str[i+j]) return 0;}return (t-s+1)/k;
}
inline int getnum(int x)
{if(x>=0&&x<=9) return 1;if(x>=10&&x<=99) return 2;return 3;
}
void solve()
{for(int i=0;i<n;++i)dp[i][i]=1;int tmp;for(int k=2;k<=n;++k){for(int i=0;i<=n-k;++i){dp[i][i+k-1]=inf;for(int j=i;j<i+k-1;++j)dp[i][i+k-1]=min(dp[i][i+k-1],dp[i][j]+dp[j+1][i+k-1]);for(int z=1;z<=k/2;++z){tmp=check(i,i+k-1,z);if(tmp)dp[i][i+k-1]=min(dp[i][i+k-1],dp[i][i+z-1]+2+getnum(tmp));}}}
}
int main()
{//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int t;scanf("%d",&t);while(t--){scanf("%s",str);n=strlen(str);solve();printf("%d\n",dp[0][n-1]);}return 0;
}

LA 3363 String Compression(dp)相关推荐

  1. LA 4394 String painter 区间DP -

    题目地址:http://vjudge.net/problem/UVALive-4394 很明显的区间DP 区间DP的套路就是 d[i][j]的在区间 (i,j) 刷的次数 转移也一般是 d[i][j] ...

  2. *【HDU - 5707】Combine String(dp)

    题干: Given three strings aa, bb and cc, your mission is to check whether cc is the combine string of  ...

  3. 【HDU - 4055】Number String(dp,思维)

    题干: The signature of a permutation is a string that is computed as follows: for each pair of consecu ...

  4. C#LeetCode刷题之#443-压缩字符串​​​​​​​(String Compression)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3943 访问. 给定一组字符,使用原地算法将其压缩. 压缩后的长度 ...

  5. [HDU3336]Count the string(KMP+DP)

    Solution 不稳定的传送门 对KMP的灵活应用 设dp[i]表示前[1,i]的答案 那么dp[i]=dp[p[i]]+1,p[i]为失配函数 Code #include <cstdio&g ...

  6. 【leetcode】443. String Compression

    题目如下: Given an array of characters, compress it in-place. The length after compression must always b ...

  7. 140.String Compression

    题目: Given an array of characters, compress it in-place. 给定一组字符,就地压缩它. The length after compression m ...

  8. LeetCode String Compression

    原题链接在这里:https://leetcode.com/problems/string-compression/description/ 题目: Given an array of characte ...

  9. HDU4055 - number string(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 思路:dp[i][j]表示处理前i个字符以j结尾可能的序列数. 当a[i]=='I'时,dp[i ...

最新文章

  1. matlab ufunc,ufunc函数
  2. c语言字面值知识体系总结大学霸IT达人
  3. delphi中的函数传参如何传枚举参数_shell脚本的函数介绍使用和工作常用案例。建议收藏...
  4. 老师学python可以干嘛-你都用 Python 来做什么?
  5. 【多线程高并发】深入理解JMM产生的三大问题【原子性、可见性、有序性】
  6. 用了Elasticsearch后,查询起飞了!
  7. 异常处理——Another app is currently holding the yum lock
  8. 使多个线程循环输出0-99-0-99
  9. 通过boundingRectWithSize:options:attributes:context:计算文本尺寸
  10. 使用mysql命令修改配置信息
  11. ae插件form_AE插件 | 没有这些插件,还想做特效?
  12. 桌面被恶意软件放上几个ie图标删除不了怎么办?
  13. python经济统计学论文_统计学论文
  14. 小米wifi驱动 linux驱动,Centos7-驱动小米WIFI做AP
  15. apt user manul
  16. 华为手机怎么导入主题_华为手机怎么安装主题?华为EMUI3.0-EMUI8.0主题安装教程...
  17. Win32 API 列表5 (格式有点乱)
  18. wmp.dll 自动注册失败
  19. Oplayer HDiPad最实用的视频播放器
  20. putty下载链接提供

热门文章

  1. Java中Iteration的用法
  2. 如何用GGGIS地图下载器下载谷歌无偏移影像和天地图标注(百度和高德地图类似)
  3. Effective Modern C++ 条款1 理解模板类型推断
  4. 基于springboot的高校社团管理系统
  5. wifi图标突然消失断网
  6. Linux驱动:基于imx6ull分析PWM驱动及backlight屏幕背光驱动
  7. Java工程师美团面试题
  8. 基于VB环境下的语音识别程序开发方法
  9. Stlink固件更新问题“ST-Link is not in the dfu mode Please restart it“的解决方法
  10. Arduino Programmable Air 可编程气动套件