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. 第三百零六节,Django框架,models.py模块,数据库操作——创建表、数据类型、索引、admin后台,补充Django目录说明以及全局配置文件配置

    Django框架,models.py模块,数据库操作——创建表.数据类型.索引.admin后台,补充Django目录说明以及全局配置文件配置 数据库配置 django默认支持sqlite,mysql, ...

  2. html -- <meta name="viewport"/>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scal ...

  3. 使用libcurl源代码编译只是的问题

    curl 7.21.6 + vs2005 就把curl的.c文件加到project中编译.报错信息非常古怪: setup_once.h(274) : error C2628: '<unnamed ...

  4. 实现QQ第三方登录教程(php)

    参看地址:http://www.bcty365.com/content-10-2945-1.html

  5. [转]oracle存储过程、声明变量、for循环

    oracle存储过程.声明变量.for循环 1.创建存储过程 create or replace procedure test(var_name_1 in type,var_name_2 out ty ...

  6. Linux配置防火墙,开启80port、3306port 可能会遇到的小问题

     vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(同意80端口通 ...

  7. ubuntu设置中文拼音输入法

    转载  http://www.cnblogs.com/zhj5chengfeng/archive/2013/06/23/3150620.html

  8. Loadrunner中socket协议中的三个关联函数

    这3个函数其实都可以动态获取运行中收到的数据包中的数据,只要跟在要获取的收取数据包脚本后面即可.其中:lrs_save_searched_string和lrs_save_param如果buf_desc ...

  9. 工作流JBPM_day02:3-预定义的活动1_4-预定义的活动2+在图片上高亮显示正在执行的上活动

    工作流JBPM_day02:3-预定义的活动1 工作流JBPM_day02:4-预定义的活动2+在图片上高亮显示正在执行的上活动 活动 Activity 预先定义好的活动 Start开始活动 End结 ...

  10. Python 入门(五)条件判断和循环

    if语句 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,可以用if语句实现: age = 20 if age > ...