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. SQLServer2008 关于数值字段列的累计

    create table #temp20110610(     id int identity(1,1),     date varchar(8),     qty float) insert int ...

  2. canvas 和 svg

    对于开发人员而言,最直观的区别在于:1.对于画在Canvas上的部件,你需要处理重绘.而SVG则不用,你修改svg dom则系统会自动帮你重绘2.Hittest,即canvas不负责帮你侦测鼠标/触摸 ...

  3. Android开放百度地图集成

    1.创建应用 获取AK (我理解为Application key)  通过百度账号登录百度地图开放平台,进入API控制台 http://lbsyun.baidu.com/apiconsole/key ...

  4. 基于 CC2530 的温度采集系统(未定稿)

    前言 最近在自学 Zigbee,每天的主要是任务是:看博客,看 CC2530 的 datasheet 和实践,熟悉片上的 SFR 以及控制板子. 学和做内容包括:IO.外部中断.Timer1/3/4. ...

  5. 错误 RC1015: 无法打开包含文件 'XTToolkitPro.rc'

    XtremeToolkitPro作为VC++/MFC平台下目前最流行的GUI界面库之一,安装和使用都很方便.不过,在实际使用中还是碰到许多问题.如果在编译工程时只显示一个错误:RC1015: 无法打开 ...

  6. SQLite 的使用

    private void button1_Click(object sender, EventArgs e) { //查询数据库内容并绑定 string sql= "select* from ...

  7. git_安装与配置

    安装 windows平台安装 在windows平台安装git,需要下载exe.文件,下载地址:https://gitforwindows.org/,双击下载的.exe文件,按照提示进行安装,安装完成后 ...

  8. Django_文件上传

    使用Django框架实现文件上传功能    upload.html <!DOCTYPE html> <html lang="en"> <head> ...

  9. day007 列表类型、元祖类型、 字典类型、 集合类型的内置方法

    目录 列表数据类型的内置方法 作用 定义方式 优先掌握的方法 需要掌握的方法 元祖类型的内置方法 作用 定义方式 优先掌握的方法(参考列表方法) 字典类型的内置方法 作用 定义方式 优先掌握的方法 需 ...

  10. luogu P2422 良好的感觉 单调栈

    Code: #include<bits/stdc++.h> #define maxn 1000000 #define ll long long using namespace std; v ...