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. ...
随机推荐
- linux常用函数简单介绍
mmap函数简介: mmap函数是unix/linux下的系统调用,来看<Unix Netword programming>卷二12.2节对mmap的介绍: The mmap functi ...
- java 1.8 新特性 stream
并发提升 java 中Stream类似于hadoop中的数据分析的思路,只不过hadoop大,用的是多台机算机的计算生态,而java stream使用的单台计算机中的多cpu分析一块数据的过程.通过 ...
- springboot使用fastJson作为json解析框架
springboot使用fastJson作为json解析框架 springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用 〇.搭建spri ...
- javaScript如何跳出多重循环break、continue
先来说说break和continue之间的区别 for(var i=0;i<10;i++){ if(i>5){ break; }}console.log(i); ---6 •当i ...
- mysql root 密码恢复
1.停止mysql服务 service mysql stop 2.启动mysql时不启动授权表,跳过权限验证使用空密码登陆 mysqld_safe --skip-grant-tables & ...
- 让R与Python共舞
转载:http://ices01.sinaapp.com/?p=129 R(又称R语言)是一款开源的跨平台的数值统计和数值图形化展现 工具.通俗点说,R是用来做统计和画图的.R拥有自己的脚本 ...
- VMW虚拟机生成的文件说明
VMDK(VMWare Virtual Machine Disk Format)是虚拟机VMware创建的虚拟硬格式,文件存在于VMware文件系统中,被称为VMFS(虚拟机文件系统) NVRAM 非 ...
- 创建 dblink
目的:oracle中跨数据库查询 两台数据库服务器db_A(本地)和db_B(远程192.168.1.100),db_A下用户user_a 需要访问到db_B下user_b的数据解决:查询 ...
- WordPress插件:自定义登录注册插件:DX Login Register
众所周知,wordpress自带的注册系统比较简单,需要接收邮件密码才能完成.不过对于国内的站长来说,会碰到不少麻烦.首先个人站长一般都使用虚拟主机,有不少还是使用国外的,你的服务器不一定会提供邮件发 ...
- jmeter-----GUI运行和非GUI运行的区别
gui:界面会消耗很多资源,并且运行的结果是保存在Jmeter运行的内存中.当时间一长,内存增长到一定程度,就会报错,甚至假死. 非gui:实时的将运行log文件保存到本地文件中,不会撑爆内存.并且对 ...