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. ...
随机推荐
- 【IDEA】IDEA设置新建文件的模板
今天在IDEA中新建JS文件的时候想着也像WebStorm一样可以显示作者和时间,所以就研究了下在IDEA中修改文件创建时的模板. 点击settings找到File and Code Template ...
- http 之cookie和session
cookie和session 关于http: 1.http是:无状态.短连接 2.http的请求生命周期:给服务端发送一个请起头,通过域名提取url,通过路由关系匹配,再通过函数+html进行模板加 ...
- jfinal文件上传
jfianl获取表单数据,需要先getFile()获取文件,再使用getPara() public class ImageUploadController extends Controller{ pu ...
- 接口测试(概念、Postman、SoapUI、jmeter)
一.什么是接口测试 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻辑 ...
- 主机名/etc/hosts文件的作用
1,/etc/hosts,主机名ip配置文件. # Do not remove the following line, or various programs # that require netwo ...
- Cookie 与 网络通信
通信协议是什么?(ftp.http.https.file...) 交流的规则 举例: 汉语.英语.法语.德语 (百度百科:通信协议) 通信协议是指双方实体完成通信或服务所必须遵循的规则和约定.通过通信 ...
- JMeter -----设置代理抓取web的HTTPS请求,“您的连接不是私密链接”的处理方案
出现如上截图的问题,已确定将网站的证书.jmeter的证书均安装完成,并未提示报错,但是在配置代理后,刷新网站抓取请求时总是提示如上报错 解决方案: 1.关闭电脑上的所有浏览器 2.打开“终端”运行: ...
- web学习测试环境
ref:https://www.owasp.org/index.php/OWASP_Vulnerable_Web_Applications_Directory_Project/Pages/Offlin ...
- [转]如何在 JS 代码中消灭 for 循环
一,用好 filter,map,和其它 ES6 新增的高阶遍历函数 二,理解和熟练使用 reduce 三,用递归代替循环(可以break!) 四,使用高阶函数遍历数组时可能遇到的陷阱 五,死磕到底,T ...
- vue 阻止表单默认事件
方式一: <form autocomplete="off" @submit.prevent="onSubmit"> <input type=& ...