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. css3动画之1--animation小例子

    1.首先看效果 2.代码及分析 <style type="text/css"> #div1 { margin:100px; position: absolute; te ...

  2. Go中的main函数和init函数

    Go里面有两个保留的函数:init函数(能够应用于所有的package)和main函数(只能应用于package main).这两个函数在定义时不能有任何的参数和返回值.虽然一个package里面可以 ...

  3. js---通过代码学习

    1:本例演示 getElementsByTagName 方法. 2:本例演示 getElementsByTagName 方法 3:注意:

  4. opengl使用FreeType绘制字体

    原文地址:http://www.cnblogs.com/zhanglitong/p/3206497.html

  5. CDC之fast->slow (1)

    Sampling slower signals into faster clock domains causes fewer potential problems than sampling fast ...

  6. 初识cocos creator的一些问题

    本文的cocos creator版本为v1.9.01.color赋值cc.Label组件并没有颜色相关的属性,但是Node有color的属性. //如果4个参数,在ios下有问题let rgb = [ ...

  7. 小程序text组件内部上边距的问题

    index.wxml: <view class="slogan"> <text> 建立跨文化的全球视野,做世界公民 </text> </v ...

  8. 完全掌握vuex

    公司项目中大量的使用了vue,感觉对vue知识的掌握也越来越熟练了,录制视频教程也让我受益匪浅,自己成长的同时,我更希望帮助其他前端小伙伴一起成长.这篇文章我们主要讲解vuex. vuex是一个专门为 ...

  9. java操作Excel的poi的导出Excel表格

    页面布局 点击导出用户:触发函数,直接访问后台 后台方法如下: public String export()throws Exception{ Connection con=null; try { c ...

  10. wget 批量下载网站目录下的文件

    执行如下命令就会自动下载 http://www.iyunwei.com/docs/ 下面的所有文件: wget -nd -r -l1 --no-parent http://www.iyunwei.co ...