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 ...
随机推荐
- jquery实现点击按钮滑动到指定位置
<body> <script type="text/javascript"> function click_scroll() { var scroll_of ...
- 嵌入式 hi3518c平台网卡模式MII与RMII模式在Uboot和kernel中切换小结
由于公司项目的需要,我们需要在原有的MII的基础上,修改为RMII模式,针对hi3518c平台,我的网卡是LAN8701需要修改的地方有如下几个: 首先我的uboot中env是: bootargs=m ...
- POJ 3977 Subset
Subset Time Limit: 30000MS Memory Limit: 65536K Total Submissions: 3161 Accepted: 564 Descriptio ...
- 正在连接...ORA-12541: TNS: 无监听程序
明明已经在Net Configuration Assistant中配置过监听程序并启动过.但在测试本地网络服务名配置中扔提示以上错误"正在连接...ORA-12541: TNS: 无监听程序 ...
- .net-C#代码判断
ylbtech-doc:.net-C#代码判断 C#代码判断 1.A,C#代码判断返回顶部 01.{ C#题目}public static void Main(string[] args){ ...
- 【Opencv 小工具】鼠标选区信息获取
有时候在目标跟踪的算法初始化工作时候,需要选区一个Rect区域,来表示要跟着的目标,所以有次小工具. 使用QT和opencv 编写 项目地址 https://github.com/wzyuliyang ...
- js控制不同的时间段显示不同的css样式
js控制不同的时间段显示不同的css样式 js函数,可以放到单独的js文件中也可以放到当前页的<head>标记之内 function getCSS(){ datetoday ...
- 【整理】Visual Studio快捷键
说明 很多开发人员使用Visual Studio的时候,由于对VS快捷键不熟悉,会影响到实际的开发效率.其实,有很多我们不知道(或已知)的快捷键,在我们熟练运用以后,能够提高我们整体的工作效率.以下是 ...
- File-nodejs
文件系统模块是一个简单包装的标准 POSIX 文件 I/O 操作方法集.您可以通过调用require('fs')来获取该模块.文件系统模块中的所有方法均有异步和同步版本. 文件系统模块中的异步方法需要 ...
- 【转】MySql数据库--mysql_real_escape_string()函数
MySql数据库--mysql_real_escape_string()函数 unsigned long mysql_real_escape_string(MYSQL *mysql, char *to ...