Folding
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1841   Accepted: 642   Special Judge

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 contains a single line of characters from 'A' to 'Z' with at least 1 and at most 100 characters.

Output

Write to the output 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

Source

题意:

给一个字符串,尽量压缩,让他长度最短。()和数字都是算长度的。所以样例里CC才没有变成2(C)

思路:

能够想到的是子结构是保存区间i,j中最短的串的长度len,以及这个最短的串

状态转移的时候我们有两种操作,一种就是简单的找一个中间的点,把两边的串合并。这个比较简单。

一种是看这个串能如何压缩。于是我们可以去枚举最后压缩了之后的子串的长度,不包括数字和括号。

对于一个区间(i, j)我们从小到大枚举压缩后的子串长度,因为压缩的越小越好。压缩完成后去比较是压缩比较好还是合并比较好。

每一次枚举区间长度和起始点。

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map>
#include<set> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; struct seg{
int len;
char str[];
}dp[][];
char s[];
int n; int main(){ while(scanf("%s", s + ) != EOF){
n = strlen(s + );
for(int i = ; i <= n; i++){
dp[i][i].len = ;
dp[i][i].str[] = s[i];
} for(int l = ; l <= n; l++){
for(int i = ; i <= n - l + ; i++){
int j = i + l - ;
dp[i][j].len = inf;
for(int nowl = ; nowl <= l / ; nowl++){//枚举子串压缩后的长度
if(l % nowl)continue;
int st = i, ed = i + nowl;
while(s[st] == s[ed] && ed <= j)st++, ed++;
if(ed > j){
int num = l / nowl;
sprintf(dp[i][j].str, "%d", num);
strcat(dp[i][j].str, "(");
strcat(dp[i][j].str, dp[i][i + nowl - ].str);
strcat(dp[i][j].str, ")");
dp[i][j].len = strlen(dp[i][j].str);
break;
}
}
for(int k = i; k < j; k++){
if(dp[i][j].len > dp[i][k].len + dp[k + ][j].len){
dp[i][j].len = dp[i][k].len + dp[k + ][j].len;
strcpy(dp[i][j].str, dp[i][k].str);
strcat(dp[i][j].str, dp[k + ][j].str);
}
}
}
} printf("%s\n", dp[][n].str);
}
return ;
}

poj2176 Folding【区间DP】的更多相关文章

  1. Codeforces Gym 100002 Problem F "Folding" 区间DP

    Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...

  2. UVA1630 Folding 区间DP

    Folding Description   Bill is trying to compactly represent sequences of capital alphabetic characte ...

  3. UVa 1630 Folding (区间DP)

    题意:折叠一个字符串,使得其成为一个尽量短的字符串  例如AAAAAA变成6(A) 而且这个折叠是可以嵌套的,例如 NEEEEERYESYESYESNEEEEERYESYESYES 会变成 2(N5( ...

  4. POJ 2176 Folding(区间DP)

    题意:给你一个字符串,请把字符串压缩的尽量短,并且输出最短的方案. 例如:AAAAA可压缩为5(A), NEERCYESYESYESNEERCYESYESYES可压缩为2(NEERC3(YES)). ...

  5. POJ2176 Folding

    POJ2176 Folding 描述 给定一个长度不超过100的字符串,求其"压缩"后长度最短的字符串.如有多个,输出任意即可. 其中对于一个字符串\(str\)的"压缩 ...

  6. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  7. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  8. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  9. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  10. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

随机推荐

  1. 第二百九十八节,python操作redis缓存-Set集合类型,可以理解为不能有重复元素的列表

    python操作redis缓存-Set集合类型,可以理解为不能有重复元素的列表 sadd(name,values)name对应的集合中添加元素 #!/usr/bin/env python # -*- ...

  2. 学习 C++,关键是要理解概念,而不应过于深究语言的技术细节

    学习 C++学习 C++,关键是要理解概念,而不应过于深究语言的技术细节. 学习程序设计语言的目的是为了成为一个更好的程序员,也就是说,是为了能更有效率地设计和实现新系统,以及维护旧系统. C++ 支 ...

  3. informatica中的workflow连接远程数据库

    如果是远程oracle这样写 名称随便起,方便自己记住,后面用户名密码你都知道,再加上数据库的地址:端口/SID就可以了. 如10.33.2.208:1521/torcl

  4. 第三章 SqlSessionFactoryBean(MyBatis)

    SqlSessionFactoryBean 在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建.而在 MyBatis-Spring 中 ...

  5. MathType如何编辑手写体l

    MathType在编辑公式不仅方便而且规范,并且能够根据自己的需要选择不同的字体进行使用,可以是正体也可以是斜体,可以是新罗马体,也可以是花体,这些用word公式编辑器MathType都是可以的.还有 ...

  6. Visual Basic的未来之路

        Green首先列出了当时使用VB进行开发的四个基础指导原则:         1.VB和C#共享的通用IDE和平台构建块.         2.共享的“多范式.面向对象.命令式.强类型等”语言 ...

  7. linux 数据盘和系统盘的查看

    系统盘就像linux的c盘,使用df -l命令查看 如下所示: 可以看到根路径 / 都是位于系统盘.而/root,/home,/usr就如同c盘下的c:\windows,c:\usr这些目录 如果单独 ...

  8. opencv移植到ubuntu

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ OpenCV 2.2以后版本需要使用Cmake生成makefile文件,因此需要先安装cmake ...

  9. POJ 1691 Painting a Board(状态压缩DP)

    Description The CE digital company has built an Automatic Painting Machine (APM) to paint a flat boa ...

  10. NodeJS-002-Expres启动

    1.打开app.js文件 2.在module.exports = app;之前输入: app.listen(8100,function(){ console.log("Server Star ...