hihocoder1320 160周 压缩字符串
hihocoder1320
题目链接
思路:
dp解法。用map[i][j]表示从第i个开始到第j个的字串的best压缩长度。(包括i,j,两端闭合)。
用k表示i,j中的一点。
用zip()表示压缩i,j字串的函数,如果字串内部不能形成循环则返回字串长度。
map[i][j] =min{ zip(map[i][j]) , map[i][k] + map[k + 1][j]};
ac代码:
// hiho160week_yasuozifuchuan.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int map[101][101];
int countbits(int a)
{
if (!a) return 1;
int count = 0;
while (a)
{
a /= 10;
count++;
}
return count;
}
int zip(string s,int start,int end)
{
if (start > end) return 0;
int len = end - start + 1;
if (len <= 4) return len;
bool flag;
for (int i = 1; i < len; i++)
{
if (len % i != 0) continue;
flag = true;
for (int j = 0; j < i; j++)
{
for(int k = j + start; k + i <= end; )
{
if (s[k] != s[k + i])
{
flag = false;
break;
}
k += i;
}
if (!flag) break;
}
if (flag)
{
return countbits(len / i) + map[start][start + i - 1] + 2;
}
}
return len;
}
int main()
{
int t;
cin >> t;
string s;
while (t--)
{
cin >> s;
int len = s.length();
////initialize
//for (int i = 0; i < len; i++)
//{
// for (int j = i; j < len; j++)
// {
// map[i][j] = j - i + 1;
// }
//}
for (int i = len - 1; i >= 0; i--)
{
for (int j = i; j < len; j++)
{
map[i][j] = j - i + 1;
if (j - i + 1 <= 4) continue;
map[i][j] = min(map[i][j], zip(s, i, j));
for (int k = i; k < j; k++)
{
map[i][j] = min(map[i][k] + map[k + 1][j], map[i][j]);
}
}
}
//for (int i = 0; i < len; i++)
//{
// for (int j = 0; j < len; j++)
// {
// if (map[i][j])
// cout << map[i][j] << "\t";
// else
// cout << " " << "\t";
// }
// cout << endl;
//}
cout << map[0][len - 1] << endl;
}
return 0;
}
hihocoder1320 160周 压缩字符串的更多相关文章
- C#压缩字符串
在论坛上看到一个压缩字符串的问题,特此记录以备后用! static string GetStringR(string inputStr) { return Regex.Replace(inputStr ...
- [LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- hihoCoder #1320 : 压缩字符串 区间dp
/** 题目:hihoCoder #1320 : 压缩字符串 链接:https://hihocoder.com/problemset/problem/1320 描述 小Hi希望压缩一个只包含大写字母' ...
- Java压缩字符串工具类
StringCompressUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.By ...
- php 压缩字符串
压缩字符串: base64_encode(gzcompress(serialize($data))) 解压字符串: unserialize(gzuncompress(base64_decode($se ...
- Java实现 LeetCode 443 压缩字符串
443. 压缩字符串 给定一组字符,使用原地算法将其压缩. 压缩后的长度必须始终小于或等于原数组长度. 数组的每个元素应该是长度为1 的字符(不是 int 整数类型). 在完成原地修改输入数组后,返回 ...
- hihocoder 1320 - 压缩字符串 - [hiho一下160周]
这道题目可以说是一道非常好非常一颗赛艇的DP题了. 需要注意的是,其中情形3),字符串必然能完全转化为 N(str)形式,如果有N(str1)M(str2)等等另外样式,应该首先使用拼接形式对其进行划 ...
- Java 压缩字符串
1.引言 最近在做项目中,平台提供一个http服务给其他系统调用,然后我接收到其他系统的json格式的报文后去解析,然后用拿到的数据去调用corba服务,我再把corba的返回值封装完成json字符串 ...
- [CareerCup] 1.5 Compress String 压缩字符串
1.5 Implement a method to perform basic string compression using the counts of repeated characters. ...
随机推荐
- 76.ZYNQ-用PS控制DDR3内存读写
本编文章的目的主要用简明的方法对DDR3进行读写,当然这种方式每次读写都需要CPU干预,效率是比较低的,但是这是学习的过程吧. 本系列文章尽可能的让每一个实验都相对独立,过程尽可能保证完整性,保证实验 ...
- 宋牧春: Linux设备树文件结构与解析深度分析(1) 【转】
转自:https://mp.weixin.qq.com/s/OX-aXd5MYlE_YoZ3p32qWA 作者简介 宋牧春,linux内核爱好者,喜欢阅读各种开源代码(uboot.linux.ucos ...
- INIT_WORK
借助runtime pm,在需要使用模块时,增加引用计数(可调用pm_runtime_get),不需要使用时,减少引用计数(可调用pm_runtime_put). 1.INIT_WORK(struct ...
- Machine Learning系列--EM算法理解与推导
EM算法,全称Expectation Maximization Algorithm,译作最大期望化算法或期望最大算法,是机器学习十大算法之一,吴军博士在<数学之美>书中称其为“上帝视角”算 ...
- Mysql自带profiling性能分析工具使用分享
1. show variables like '%profiling%';(查看profiling信息) 2. set profiling=1;(开启profiling) 3. 执行S ...
- 用js实现图片连播和联级菜单的实现
<!DOCTYPE html> <html> <head> <title>图片轮播</title> <style> div{ b ...
- 20165301 预备作业三:Linux安装及命令入门
预备作业三:Linux安装及命令入门 VirtualBox虚拟机的安装 在进行安装之前,原本以为有了娄老师的安装教程会是一件很容易的事情.万万没想到,在自己实际动手操作中,还是遇到了许多困难.通过与同 ...
- 高屋建瓴之WebMail攻与防
0x01:前言 随着互联网的快速发展,我们的生活与互联网的联系愈加的紧密.各种快捷方便的信息化通信工具渐渐取代了传统的通信方式.微博.QQ.MSN.微信.陌陌, …这样的社交软件和平台已经成为了我们生 ...
- js获取url链接地址的参数
访问地址为:http://XXX.com?style=green <script language="javascript"> var getArgs = functi ...
- Socket编程(一):建立与客户端的连接并接受数据
我们这里利用Socket在模拟一个客户端与服务器通信,其实客户端与服务端通信就像人与人打电话一样,想要给一个人打电话,我们首先必须要有手机,必须知道对方的手机号码,这里Socket就好比一部手机,而短 ...