【Link】:

【Description】



你能对字符串进行压缩的操作;

即把连续出现的相同的子串改成它出现的次数+这个最基本的字符串的形式;

问你这个字符串最短能被压缩得多短;

【Solution】



设f[i][j]表示,i..j这一段最短能压缩得多短;

d[i][j]表示i..j这一段最短的形式压缩成的字符串是什么;

对于一段i..j

有两种可能

1.是两个压缩串合并起来的;

2.自己构成一个压缩串

对于第一种,枚举间断点;

对于第二种,看看最短的母串是什么,用最短的母串构造压缩;

看看间断点自己构成重复的,哪一种更优;

选择最优的就好;

构造的时候,里面的东西也要是被压缩过的,因为压缩可以嵌套



【NumberOf WA】



0



【Reviw】



这是一种模型;

即是由两个串并在一起,还是自己组成压缩串.

题目中所给的定义很有用.



【Code】

#include <bits/stdc++.h>
using namespace std;
const int N = 100;
const int INF = 0x3f3f3f3f; string s,d[N+10][N+10];
int f[N+10][N+10];
int n; int ok(int l,int r){
for (int i = 1;i <= r-l;i++)
if ( (r-l+1)%i==0){
bool ok = true;
for (int j = l;j+i <= r;j++)
if (s[j]!=s[j+i])
ok = false;
if (ok) return i;
}
return 0;
} int dfs(int l,int r)
{
if (f[l][r]!=-1) return f[l][r];
if (l==r){
f[l][r] = 1;
d[l][r]="";d[l][r] += s[l];
return f[l][r];
}
int mi = INF,pos;
for (int i = l;i <= r-1;i++){
int temp = dfs(l,i) + dfs(i+1,r);
if (temp < mi){
mi = temp;
pos = i;
}
}
d[l][r] = d[l][pos]+d[pos+1][r];
int len = ok(l,r);
if (len){
int tlen = (r-l+1)/len;
ostringstream ts;
ts << tlen;
string temp = ts.str();
temp+="("+d[l][l+len-1]+")";
if ((int)temp.size()<mi){
d[l][r] = temp;
mi = (int) temp.size();
}
}
return f[l][r] = mi;
} int main(){
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
while (cin >> s){
n = s.size();
s = ' '+s;
memset(f,-1,sizeof f);
dfs(1,n);
cout << d[1][n] <<endl;
}
return 0;
}

【Uva 1630】Folding的更多相关文章

  1. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  2. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  3. 【UVa 10881】Piotr's Ants

    Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...

  4. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. 【POJ 2176】Folding

    [原题链接]传送门 [题面大意] 一个字符串,可以将它改写成循环节带括号的形式进行压缩,输出压缩长度最小的字符串. [题解思路] 1.没思路没思路,不知道怎么乱搞,大概就可以想到动态规划. 2.套路区 ...

  9. 【Uva 11584】Partitioning by Palindromes

    [Link]:https://cn.vjudge.net/contest/170078#problem/G [Description] 给你若干个只由小写字母组成的字符串; 问你,这个字符串,最少能由 ...

随机推荐

  1. _00020 妳那伊抹微笑_谁的异常最诡异第一期之 SqlServer RSA premaster secret error

    博文作者:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 博文标题:_00020 妳那伊抹微笑_谁的异常最诡异第一期之 SqlServer RSA premas ...

  2. bzoj1193: [HNOI2006]马步距离(贪心+bfs)

    1193: [HNOI2006]马步距离 题目:传送门 题解: 毒瘤题... 模拟赛时的一道题,刚开始以为是一道大难题...一直在拼命找规律 结果.... 还是说正解吧: 暴力的解法肯定是直接bfs, ...

  3. MongoDB Shell (mongo)

    https://docs.mongodb.com/getting-started/shell/client/ The mongo shell is an interactive JavaScript ...

  4. rest_framework 认证功能

    from django.views import View from rest_framework.views import APIView from rest_framework.authentic ...

  5. [NOI2002] Savage 解题报告(扩展欧几里得)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1407 Description 克里特岛以野人群居而著称.岛上有排列成环行的M个山洞.这些 ...

  6. 虚拟主机TOMCAT配置

    在tomcat中添加虚拟主机: 编辑"tomcat\conf\server.xml",在"<Engine></Engine>"元素中新加 ...

  7. Playing With Stones UVALive - 5059 Nim SG函数 打表找规律

    Code: #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...

  8. myisam和innodb的qubie

    1.Myisam 支持锁表,innoDB 支持行锁. 2.innoDB 和 BDB 支持事务. 3.Myisam 与 innoDB 索引的区别:      Myisam 无论是主键索引还是其他索引,索 ...

  9. NodeJS学习笔记 (17)集群-cluster(ok)

    cluster模块概览 node实例是单线程作业的.在服务端编程中,通常会创建多个node实例来处理客户端的请求,以此提升系统的吞吐率.对这样多个node实例,我们称之为cluster(集群). 借助 ...

  10. windows用xstart远程连接linux图形用户界面

    转载:https://blog.csdn.net/yabingshi_tech/article/details/51839379 双击xstart 输入:/usr/bin/xterm -ls -dis ...