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 ...
随机推荐
- Android开发中常见的设计模式
对于开发人员来说,设计模式有时候就是一道坎,但是设计模式又非常有用,过了这道坎,它可以让你水平提高一个档次.而在android开发中,必要的了解一些设计模式又是非常有必要的.对于想系统的学习设计模式的 ...
- delphi中计算指定日期是该月第几周的函数
NthDayOfWeek 计算并返回指定日期是该月第几周 Unit:DateUtils function NthDayOfWeek(const AValue: TDateTime): Word; ...
- iOS开发:AFNetworking、MKNetworkKit和ASIHTTPRequest比较
转:http://www.xue5.com/Mobile/iOS/747036.html 之前一直在使用ASIHTTPRequest作为网络库,但是由于其停止更新,iOS7上可能出现更多的问题,于是决 ...
- csv文件与DataTable互相导入处理
封装处理下,以后项目用到可以直接使用,比较简单. 1.首先看封装好的类 using System; using System.Data; using System.IO; using System.T ...
- [CODEVS2603]公路修建
题目描述 某国有n个城市,它们互相之间没有公路相通,因此交通十分不便.为解决这一“行路难”的问题,政府决定修建公路.修建公路的任务由各城市共同完成. 修建工程分若干轮完成.在每一轮中,每个城市选 ...
- DOM笔记(二):Node接口
所有的节点都使用Node接口来表示,可以使用很多方法去获取节点,如document.getElementsByTagName().document.getElementsByName()等均返回一个N ...
- 【Hadoop代码笔记】Hadoop作业提交之Child启动map任务
一.概要描述 在上篇博文描述了TaskTracker启动一个独立的java进程来执行Map或Reduce任务.在本篇和下篇博文中我们会关注启动的那个入口是org.apache.hadoop.mapre ...
- Distributed Sentence Similarity Base on Word Mover's Distance
Algorithm: Refrence from one ICML15 paper: Word Mover's Distance. 1. First use Google's word2vec too ...
- append some buttons to the standard datagrid pager bar
<script type="text/javascript"> $(function(){ var pager = $('#dg').datagrid('getP ...
- homework-附加题:第12章基本数据类型阅读总结
基本数据类型是构建其他所有数据类型的构造块,本人认为这部分是计算机编程的基础,值得得到大家的注意. 首先,在本章中作者提到了避免使用magic number.使用magic number这种做法是极其 ...