UVa 1630 Folding (区间DP)
题意:折叠一个字符串,使得其成为一个尽量短的字符串 例如AAAAAA变成6(A)
而且这个折叠是可以嵌套的,例如 NEEEEERYESYESYESNEEEEERYESYESYES 会变成 2(N5(E)R3(YES))。
析:用dp[i][j] 表示字符串中的第 i 个到第 j 个字符压缩后的最短长度。那么就有两种方式,一种就是自身压缩都最短,另一种就是两段分别压缩,
然后再接起来最短。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e2 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
string str;
string dp[maxn][maxn]; int cal(int l, int r){
int len = (r - l + 1);
for(int i = 1; i <= len/2; ++i) if(len % i == 0){
bool ok = true;
for(int j = l; j <= r-i; ++j) if(str[j] != str[j+i]){
ok = false; break;
}
if(ok) return i;
}
return 0;
} void solve(){
for(int i = 0; i < n; ++i) dp[i][i] = str[i];
for(int i = n-2; i >= 0; --i){
for(int j = i+1; j < n; ++j){
int ans = INF, x;
for(int k = i; k < j; ++k) if(ans > dp[i][k].size() + dp[k+1][j].size()){
ans = dp[i][k].size() + dp[k+1][j].size();
x = k;
}
dp[i][j] = dp[i][x] + dp[x+1][j];
int len = cal(i, j);
if(len){
char s[5];
sprintf(s, "%d", (j-i+1)/len);
string tmp = (string)s + "(" + dp[i][i+len-1] + ")";
if(tmp.size() <= ans) dp[i][j] = tmp;
}
}
}
} int main(){
while(cin >> str){
n = str.size();
solve();
cout << dp[0][n-1] << endl;
}
return 0;
}
UVa 1630 Folding (区间DP)的更多相关文章
- Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...
- UVA1630 Folding 区间DP
Folding Description Bill is trying to compactly represent sequences of capital alphabetic characte ...
- hdu 4597 + uva 10891(一类区间dp)
题目链接:http://vjudge.net/problem/viewProblem.action?id=19461 思路:一类经典的博弈类区间dp,我们令dp[l][r]表示玩家A从区间[l, r] ...
- UVA - 1632 Alibaba (区间dp+常数优化)
题目链接 设$dp[l][r][p]$为走完区间$[l,r]$,在端点$p$时所需的最短时间($p=0$代表在左端点,$p=1$代表在右端点) 根据题意显然有状态转移方程$\left\{\begin{ ...
- UVA 10559 Blocks——区间dp
题目:https://www.luogu.org/problemnew/show/UVA10559 应该想到区间dp.但怎么设计状态? 因为连续的东西有分值,所以应该记录一下连续的有多少个. 只要记录 ...
- UVA 10559 Blocks —— 区间DP
题目:https://www.luogu.org/problemnew/show/UVA10559 区间DP,有点难想: 为了方便,先把原来就是连续一段相同颜色的点看做一个点,记一下长度: f[i][ ...
- UVA - 1632 Alibaba 区间dp
题意:给定n个点,其中第i个点的坐标是,且它会在秒后消失.Alibaba可以从任意位置出发,求访问完所有点的最短时间.无解输出No solution. 思路:表示访问完区间后停留在i点的最短时间,表示 ...
- UVA 10559 Blocks(区间DP&&递推)
题目大意:给你玩一个一维版的消灭星星,得分是当前消去的区间的长度的平方,求最大得分. 现在分析一下题目 因为得分是长度的平方,不能直接累加,所以在计算得分时需要考虑前一个状态所消去的长度,仅用dp[l ...
- UVA - 1630 Folding(串折叠)(dp---记忆化搜索)
题意:给出一个由大写字母组成的长度为n(1<=n<=100)的串,“折叠”成一个尽量短的串.折叠可以嵌套.多解时可输出任意解. 分析: 1.dp[l][r]为l~r区间可折叠成的最短串的长 ...
随机推荐
- eclipse mingw cpp开发环境
Eclipse开发c++ 对比:微软的VC++6.0:太老了,对win7兼容不好, 现在微软的Visual Studio:安装包太大,好几个G,装了一堆你不需要的东西,要钱,教 育版申请麻烦 DOS下 ...
- iOS开发:本地数据存储-NSUserDefaults
Getting Default Values arrayForKey(_:) boolForKey(_:) dataForKey(_:) dictionaryForKey(_:) floatForKe ...
- codevs 3732 解方程
神题不可言会. f(x+p)=f(x)(mod p) #include<iostream> #include<cstdio> #include<cstring> # ...
- ionic安装拍照选照片插件
1.安装插件,也可以用ionic plugin add .... phonegap local plugin add https://git-wip-us.apache.org/repos/asf/c ...
- 原创:js代码, 让dedecms支持Tag选择, 添加内容更为方便,不用手输Tag
dedecms在编辑修改内容时,TAG标签需要手动输,中文的Tag, 中间还得用半角字符','分隔, 输入法切来切去很不方便, 于是动手改后台代码, 利用后台的tags_main.php, 让d ...
- linux下查看串口信息
rs232串口通信接口:当通信距离较近时(<12m),可以使用电缆线直接连接,若距离较远,需附加调制解调器. 9个脚针的定义: CDC数据载波检测,RXD接收数据,TXD发送数据,DTR数据中断 ...
- 【转】pdf 中如何把几页缩小成一页打印
我用的是Foxit PDF Reader,可以这样设置:文件-打印-打印处理下的页面排列选择“在每张纸上放置多页”-选择每页版数即可. 如果你用的是Adobe Reader,也可以自己找一下,看是否有 ...
- hdu 1024(最大和连续子序列增强版)
题意:最大和连续子序列的增强版,要求从一序列中取出若干段,这些段之间不能交叉,使得和最大并输出. 分析:用dp[i][j]表示前j个数取出i段得到的最大值,那么状态转移方程为dp[i][j]=max( ...
- MyBatis 入门到精通(一) 了解MyBatis获取SqlSession
MyBatis是什么? MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis ...
- Cutting Sticks
题意: l长的木棒,给出n个切割点,每切一次的费用为切得木棒的长度,完成切割的最小费用. 分析: 区间dp入门,区间dp的特点,一个大区间的解可以转换成小区间的解组合起来,每个切割点的标号代表边界. ...