Folding

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

Input file contains several test cases, one per line. Each of them contains a single line of characters from `A' to `Z' with at least 1 and at most 100 characters.

 output

For each input case write a different output line. This must be 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 intput

AAAAAAAAAABABABCCD
NEERCYESYESYESNEERCYESYESYES

  sample output

9(A)3(AB)CCD
2(NEERC3(YES)) 题意:
  
  给你一串字符串,让你简化 题解:
  
  区间dp
  有几个需要注意的点,就是简化后加上个数和两个()可能会比原来的子串还要长。
#include<bits/stdc++.h>
using namespace std;
const int INF= 0x3f3f3f3f;
string str;
int DP[][];
string fold[][];
int judge(int l,int r){
for(int i=;i<=(r-l+)/;i++)
{
if((r-l+)%i) continue;
bool flag=true;
for(int j=l;j+i<=r;j++)
{
if(str[j]!=str[j+i])
{
flag=false;
break;
}
}
if(flag) return i;
}
return false;
}
int fun(int l,int r){
if(DP[l][r]!=-) return DP[l][r];
if(l==r){
DP[l][r]=;
fold[l][r]=str[l];
return ;
}
int k;
int re=INF;
for(int i=l;i<r;i++)
{
int tmp=fun(l,i)+fun(i+,r);
if(tmp < re) { k=i; re=tmp; }
}
fold[l][r]=fold[l][k]+fold[k+][r];
int len=judge(l,r);
if(len){
char t[];
sprintf(t,"%d",(r-l+)/len); //对于一个超过十的整数快速将他转化为字符串形式
string newstr=t+string("(")+fold[l][l+len-]+string(")");
if(newstr.size()<re){
re=newstr.size();
fold[l][r]=newstr;
}
}
DP[l][r]=re;
return re;
}
int main() {
while(cin>>str){
int R=str.size()-;
memset(DP,-,sizeof(DP));
fun(,R);
cout<<fold[][R]<<endl;
}
return ;
}

UVA1630 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. UVa 1630 Folding (区间DP)

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

  3. POJ 2176 Folding(区间DP)

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

  4. UVa1630,Folding

    区间dp,记忆化搜就可以 st为原串 dp[p][q]存st[p]~st[q]的最优长度,f[p][q]存对应的最优串 从(0,len-1)开始搜,f[0][len-1]为所求ans,回溯条件为p== ...

  5. 【BZOJ-4380】Myjnie 区间DP

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

  6. 【POJ-1390】Blocks 区间DP

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

  7. 区间DP LightOJ 1422 Halloween Costumes

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

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

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

  9. poj2955 Brackets (区间dp)

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

随机推荐

  1. [Luogu 2216] [HAOI2007]理想的正方形

    [Luogu 2216] [HAOI2007]理想的正方形 题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输 ...

  2. C - Tram

    Problem description Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n i ...

  3. ASP.NET MVC + 工厂模式 + 三层 + 缓存

    最近将手头的项目总结整理了一下,以方便自己的学习.... 下面直接上图先介绍项目的结构图: 项目是ASP.NET MVC 4.0的应用程序,DBUtility这个类库主要是DbHelper操作数据库的 ...

  4. dubbo之只订阅及只注册

    只订阅 问题 如果有两个镜像环境,两个注册中心,有一个服务只在其中一个注册中心有部署,另一个注册中心还没来得及部署,而两个注册中心的其它应用都需要依赖此服务,所以需要将服务同时注册到两个注册中心,但却 ...

  5. react获取url查询参数

    继承自React.Component的this.props.location.query对象下有当前url的各种查询参数.简单的例子:在控制台打印这个对象 import React from 'rea ...

  6. phpStudy 升级 MySQL版本

    1:停止phpStudy ,Mysql 服务;删除Mysql 文件夹 替换为新版本的Mysql 2:复制一份 my-default.ini,改名 my.ini ,打开,在最后面加上: basedir= ...

  7. 关于计算文字显示占用画面大小(System.Drawing.Graphics.MeasureString)

    最近遇到了一个需要手动为显示文字换行的场合,网上转了一圈,最后形成了下面的代码: var font = new Font("微软雅黑", 9F); - DETAIL_BASE_IN ...

  8. 下拉框处理(select)

    在UI自动化测试过程中,经常会遇到一些下拉框,我们有三种可选方式来操作下拉框. 第一种方法 基于webdriver的两次click,很容易出现问题,不建议使用.(由于部分下拉框在点击一次后,失去焦点再 ...

  9. The story of one latency spike

    转自:https://blog.cloudflare.com/the-story-of-one-latency-spike/ A customer reported an unusual proble ...

  10. vue.js层叠轮播

    最近写公司项目有涉及到轮播banner,一般的ui框架无法满足产品需求:所以自己写了一个层叠式轮播组件:现在分享给大家: 主要技术栈是vue.js ;javascript;jquery:确定实现思路因 ...