Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding"
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100002
Description
Bill is trying to compactly represent sequences of capital alphabetic characters from 'A' to 'Z' by folding repeating subsequences inside them. For example, one way to represent a sequence AAAAAAAAAABABABCCD is 10(A)2(BA)B2(C)D. He formally defines folded sequences of characters along with the unfolding transformation for them in the following way:
- A sequence that contains a single character from 'A' to 'Z' is considered to be a folded sequence. Unfolding of this sequence produces the same sequence of a single character itself.
- If S and Q are folded sequences, then SQ is also a folded sequence. If S unfolds to S' and Q unfolds to Q', then SQ unfolds to S'Q'.
- If S is a folded sequence, then X(S) is also a folded sequence, where X is a decimal representation of an integer number greater than 1. If S unfolds to S', then X(S) unfolds to S' repeated X times.
According to this definition it is easy to unfold any given folded sequence. However, Bill is much more interested in the reverse transformation. He wants to fold the given sequence in such a way that the resulting folded sequence contains the least possible number of characters.
Input
The input file contains a single line of characters from 'A' to 'Z' with at least 1 and at most 100 characters.
Output
Write to the output file a single line that contains the shortest possible folded sequence that unfolds to the sequence that is given in the input file. If there are many such sequences then write any one of them.
Sample Input
AAAAAAAAAABABABCCD
Sample Output
9(A)3(AB)CCD
HINT
题意
就是相同的可以被压缩,问你最少压缩成什么样子(注意,括号和数字也算长度
题解:
区间DP,维护区间DP的时候,同时维护一下这个区间的字符串是什么样子的就好了
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 110000
#define mod 10007
#define eps 1e-9
#define pi 3.1415926
int Num;
//const int inf=0x7fffffff; //§ß§é§à§é¨f§³
const ll Inf=0x3f3f3f3f3f3f3f3fll;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** string s[][];
int n;
int dp[][];
int vis[][];
char S[];
int FF(int x)
{
int add=;
while(x)
{
add++;
x/=;
}
return add;
}
string F(int x)
{
string ss;
while(x)
{
ss+=(char)(x%+'');
x/=;
}
reverse(ss.begin(),ss.end());
return ss;
}
int solve(int l,int r)
{
if(vis[l][r])return dp[l][r];
vis[l][r]=;
for(int i=l;i<r;i++)
{
if(dp[l][r]>solve(l,i)+solve(i+,r))
{
dp[l][r]=dp[l][i]+dp[i+][r];
s[l][r]=s[l][i]+s[i+][r];
}
}
for(int i=;i<r-l+;i++)
{
if((r-l+)%(i)!=)
continue;
int add = ;
for(int k=;;k++)
{
if((k+)*i>r-l+)
break;
for(int j=;j<i;j++)
{
if(S[l+k*i+j]!=S[l+j])
break;
if(j==i-)
add+=;
}
} if(add==(r-l+)/i)
{
int point=solve(l,l+i-)++FF(add);
if(dp[l][r]>point)
{
dp[l][r]=point;
s[l][r]="";
s[l][r]+=F(add)+'(';
s[l][r]+=s[l][()*i-+l];
s[l][r]+=')';
}
}
}
return dp[l][r];
}
int main()
{
freopen("folding.in","r",stdin);
freopen("folding.out","w",stdout);
scanf("%s",S+);
n = strlen(S+);
for(int i=;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
dp[i][j]=j-i+;
for(int k=;k<j-i+;k++)
{
s[i][j]+=S[i+k];
}
}
}
solve(,n);
cout<<s[][n]<<endl;
return ;
}
Codeforces Gym 100002 Problem F "Folding" 区间DP的更多相关文章
- Codeforces Gym 100286F Problem F. Fibonacci System 数位DP
Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...
- Codeforces Gym 100500F Problem F. Door Lock 二分
Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- Codeforces Gym 100002 D"Decoding Task" 数学
Problem D"Decoding Task" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- Codeforces Gym 100002 B Bricks 枚举角度
Problem B Bricks" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100002 ...
- Codeforces Gym 100002 E "Evacuation Plan" 费用流
"Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- Codeforces Gym 100002 C "Cricket Field" 暴力
"Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1000 ...
- Codeforces 758D Ability To Convert(区间DP)
题目链接:http://codeforces.com/problemset/problem/758/D 题意:一个n进制下的数k,其中k不会用字母,如果有A就用10代替了.求k这个数对应的,在10进制 ...
- UVA1630 Folding 区间DP
Folding Description Bill is trying to compactly represent sequences of capital alphabetic characte ...
随机推荐
- 把一个类(或者Object)转换成字典
直接上代码:把一个类转换成object,然后在转换成字典 internal static IDictionary<string, string> GetDictionary(this ob ...
- 我的ECshop二次开发从零开始
我是一个EC新手,EC就算做再多的模板,肯定也满足不了我们的需要,更何况各行有各行的门道,EC统一做出来的模板也不一定合适于我们这个行业用,因此,只有我们真正掌握了自己做模板,修改模板的功夫,才能真正 ...
- ASP.NET缓存策略经验谈
要提升ASP.NET应用程序的性能,最简单.最有效的方式就是使用内建的缓存引擎.虽然也能构建自己的缓存,但由于缓存引擎已提供了如此多的功能,所以完全不必如此麻烦.在很大程度上,ASP.NET开发者在W ...
- 构建属于自己的ORM框架之二--IQueryable的奥秘
上篇文章标题乱起,被吐槽了,这次学乖了. 上篇文章中介绍了如何解析Expression生成对应的SQL语句,以及IQueryable的一些概念,以及我们所搭建的框架的思想等.但还没把它们结合并应用起来 ...
- SQL Server 高性能写入的一些总结(转)
1.1.1 摘要 在开发过程中,我们不时会遇到系统性能瓶颈问题,而引起这一问题原因可以很多,有可能是代码不够高效.有可能是硬件或网络问题,也有可能是数据库设计的问题. 本篇博文将针对一些常用的数据库性 ...
- ASP.NET 中JSON 的序列化和反序列化
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...
- 关于Cocoapods安装与问题
安装: 1.打开终端 2.如果网络没有FQ的话,需要通过淘宝的RubyGems镜像进行安装. 首先移除默认地址: gem sources --remove https://rubygems.org/ ...
- [Hive - LanguageManual] Describe
Describe Describe Database Describe Table/View/Column Display Column Statistics Describe Partition D ...
- Maven管理多模块项目
首先,我们要明确的多模块项目的含义,它是指一个应用中包含多个module.一般来说,一个应用单独部署成服务,只是打包的时候,maven会把各个module组合在一起.各模块一般单独打成jar放到lib ...
- CentOS 7 最小化安装之后安装Mysql
启动网卡 验证网络管理器和网卡接口状态 # systemctl status NetworkManager.service # nmcli dev status 修改网卡配置文件 通过上一步可以看到有 ...