UVA 1630 Folding
题目链接:http://acm.hust.edu.cn/vjudge/problem/51191
题意:给一个字符串,相同部分可以折叠,折叠可以嵌套。求最短长度的一种折叠方法。括号和数字的长度也要考虑进去。
思路:对于一个字符串,有三种策略:1、不折叠。2、本身可以折叠。3、分为两个区间子问题。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
#define Clean(x,y) memset(x,y,sizeof(x))const int maxn = 109;char s[maxn];
int n;int dp[maxn][maxn];
int type[maxn][maxn]; // 0不折叠 负数表示串自己折叠,折叠长度为值的相反数 , 正数表示分为两个区间,记录第一个区间的长度
bool flag[maxn][maxn];
int dfs(int l,int r);int f( int x ) //计算x的位数
{int ans = 0;while( x ) ans++ , x/=10;return ans;
}int check( int l , int r , int &x ) //检查[l,r]是否可以折叠,并求出最短长度
{int ans = 9999;int L = r - l + 1;rep(len,1,L-1)if ( L % len == 0 ) //尝试len长度的折叠{bool flag = true;int p1 = l , p2 = l + len;while( p2 <= r ){if ( s[p1] != s[p2] ) {flag = false;break;}p1++ , p2++;}if ( flag ) //可以折叠{int temp = f( L / len ) + 2 + dfs( l , l + len - 1 ); //包括括号和前面的次数一共的最短长度if ( temp < ans ){ans = temp;x = len; //x为循环长度}}}return ans;
}int dfs( int L , int R )
{if ( flag[L][R] ) return dp[L][R];flag[L][R] = true;if ( L == R ){type[L][R] = 0;return dp[L][R] = 1;}int &ans = dp[L][R];ans = R - L + 1;type[L][R] = 0;int x;int temp = check(L,R,x);if ( ans > temp ){ans = temp;type[L][R] = -x;}int len = R - L;rep(i,1,len){temp = dfs( L , L + i - 1 ) + dfs( L + i , R );if ( temp < ans ){ans = temp;type[L][R] = i;}}return dp[L][R];
}void out( int l , int r )
{if ( type[l][r] == 0 ){rep(i,l,r) putchar(s[i]);return;}if ( type[l][r] < 0 ){printf("%d",(r-l+1)/(-type[l][r]) );putchar('(');out(l,l-type[l][r]-1);putchar(')');return;}out( l , l + type[l][r] - 1 );out( l + type[l][r] , r );
}int main()
{while( gets(s+1) ){Clean(flag,false);n = strlen(s+1);dfs( 1 , n );out( 1 , n );puts("");}return 0;
}
UVA 1630 Folding相关推荐
- uva 1630——Folding
题意:给出一个由大写字母组成的长度为n的串,然后尽量折叠成一个尽量短的串,折叠可以嵌套. 思路:区间dp,dp(i,j)表示区间(i,j)的最短的串的长度,as(i,j)表示i到j的答案,有两个状态要 ...
- Uva 1630 折叠串
题目链接:https://uva.onlinejudge.org/external/16/1630.pdf 题意:折叠串,给一个字符串,相同部分可以折叠,折叠可以嵌套.求最短长度的一种折叠方法.括号和 ...
- 紫书《算法竞赛入门经典》
紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...
- [搜索]UVa 129 困难的串
题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...
- uva 401.Palindromes
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Uva 3767 Dynamic len(set(a[L:R])) 树套树
Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...
- UVA 11752 超级幂
UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & ...
- UVa 11174 - Stand in a Line
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 10112 - Myacm Triangles
UVa第一卷最后一题. 求内部不含点并且面积最大的三角形. 暴力. 代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #inclu ...
最新文章
- python入门(一)
- struct 与 class区别
- 【企业管理】价值创造的来源
- python浮点数类型与数学_Python3标准库:decimal定点数和浮点数的数学运算
- python一个类有几个实例对象_Python为单个对象/类创建多个实例
- python处理pdf实例_python使用pdfminer解析pdf文件的方法示例
- 专用efi文件包_优质设备项目来啦!山钢焦炉、济南鲍德炉料公司所属资产包转让...
- eclipse编码方式、标签语言获取根目录
- 重构:从方法论到实践
- Podfile、Podfile.lock、Manifest.lock、Podspec
- BMC bioinformatics research
- 学习:网络接口RJ45
- 索尼的hlg是什么_索尼摄像机上的hlg是什么意思
- 计算机二级vf上机考试题库,计算机等级考试二级VF上机题库
- Ice helloworld
- Emacs基础使用教程及常见命令整理
- 数据结构课程设计-推箱子
- PromQL 直方图 跟踪请求的延迟或响应大小 99%的请求是在多少延迟下完成的?
- python技术学什么好呢_想自学一些实用的技术,学什么好?
- 使用RSA、MD5对参数生成签名与验签