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. ...
随机推荐
- perl6检测网站CMS脚本(测试代码)
代码如下: use HTTP::UserAgent; use JSON::Tiny; my $check-url = 'www.baidu.com'; #say @*ARGS[0]; #检测命令行参数 ...
- 011 CountDownLatch,CyclicBarrier和Semaphore
CountDownLatch(闭锁,有译倒计数,锁寄存): public class CountDownLatchTest { /*** 比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此 ...
- libuv 一 环境搭建, hello TTY
引言 - 一时心起, libuv linux 搭建 有一天突然想起来想写个动画. 找了一下 ui 库太大. 后面想起以前弄过的 libuv. 但发现 libuv 相关资料也很少. 所以就有了这些内容. ...
- 《深入理解Java虚拟机》笔记--第二章、Java内存区域与内存溢出异常
Java程序员把内存的控制权交给了Java虚拟机.在Java虚拟机内存管理机制的帮助下,程序员不再需要为每一个new操作写对应的delete/free代码,而且不容易出现内存泄露和溢出. 虚拟机在执行 ...
- php 学习try_catch测试抛出异常
/** * Class show * 一个catch接收抛出异常 */ class show { // 错误的演示 //try { //require ('test_try_catch.php'); ...
- [你必须知道的.NET]第二十一回:认识全面的null
发布日期:2008.7.31 作者:Anytao © 2008 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 说在,开篇之前 null.nullable.??运算符.null ...
- 面试题32:从1到n整数中1出现的次数
这是一道广为流传的google面试题.用最直观的方法求解并不是很难,但遗憾的是效率不是很高:而要得出一个效率较高的算法,需要比较强的分析能力,并不是件很容易的事情.当然,google的面试题中简单的也 ...
- html禁止浏览器默认行为,让页面更像应用。
在html或body行内写入:oncontextmenu="return false" ondragstart='return false;' onselectstart=&quo ...
- ubuntu访问win10下的磁盘
在ubuntu下访问win10的磁盘时,提示出错. 先用命令查看一下磁盘挂载情况. sudo fdisk -l 会看到一些信息 我要访问的是E盘,也就是 /dev/sda6 这个分区 使用命令ntfx ...
- lr_start_transaction/lr_end_transaction事物组合
lr_start_transaction/lr_end_transaction事物组合 总结一下: lr_start_transaction与lr_end_transaction 为使用最多的事物创造 ...