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. ...
随机推荐
- 使用maven打包项目遇到错误: http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
今天在使用maven打包项目时遇到一个错误: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin ...
- LDA线性判别分析
LDA线性判别分析 给定训练集,设法将样例投影到一条直线上,使得同类样例的投影点尽可能的近,异类样例点尽可能的远,对新样本进行分类的时候,将新样本同样的投影,再根据投影得到的位置进行判断,这个新样本的 ...
- 高通msm mdm 总结
1. svn 获取工程代码命令:svn co svn+ssh://10.20.30.18/svn-repos/msm8916/branches/LA1.1-CS-r113502.2 2. 如何确定那些 ...
- linux删除第几天日志【原创】
cat del.sh #!/bin/bash thirty=`date -d '30days ago' +%Y-%m-%d` cd $ #删除输入路径下第30天的日志文件 find . -name & ...
- 012 public等关键字可见性
public: 具有最大的访问权限,可以访问任何一个在classpath下的类.接口.异常等.它往往用于对外的情况,也就是对象或类对外的一种接口的形式. protected: 主要的作用就是用来保护子 ...
- python安装模块的时候报错error: command 'gcc' failed with exit status 1
[情况] 在写Python代码的时候,需要用到psutil模块,需要安装. 但是在安装时,报错:error: command 'gcc' failed with exit status 1 [解决步骤 ...
- Oracle Spatial操作geometry方法
Oracle Spatial中SDO_GEOMETRY类型: CREATE TYPE SDO_GEOMETRY AS OBJECT( SDO_GTYPE NUMBER,--几何类型,如点线面 SDO_ ...
- yolo回归型的物体检测
本弱又搬了另外一个博客的讲解: 缩进YOLO全称You Only Look Once: Unified, Real-Time Object Detection,是在CVPR2016提出的一种目标检测算 ...
- 创建文件和修改时间戳——touch
linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1.命令格式: touch [选项]... 文件... 2.命令参数: -a ...
- CSU 1355 地雷清除计划
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1355 好题,根本想不到是网络流. 模型如图: 假想从右上角到左下角有一条阻拦线,我们就是 ...