UVa1630 Folding 记忆化搜索
来源:互联网 发布:centos修改ip地址 编辑:程序博客网 时间:2024/06/10 11:20
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, thenSQ is also a folded sequence. IfS unfolds toS' andQ unfolds toQ', thenSQ unfolds toS'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. IfS unfolds toS', thenX(S) unfolds toS' repeatedX 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 Input
AAAAAAAAAABABABCCDNEERCYESYESYESNEERCYESYESYES
Sample Output
9(A)3(AB)CCD2(NEERC3(YES))
这题目的思路就是看当前的字符串是否可以折叠,如果可以就看折叠后的长度时候比之前的长度要小,用dp[i][j]表示字符串从i~j的经过折叠后的结果,如果不可以折叠,就搜寻最优的切割k使得dp[i][k].size()+dp[k+1][j].size()最小,再把结果保存下来就可以。
#include <iostream> #include <string> #include <cstdio> #include <cstring> using namespace std; const int maxn = 105; string tmp; string dp[maxn][maxn]; int isfold (int i , int j) { int len = j-i+1; for (int k = 1 ; k <= len/2 ; k++) { if (len%k) continue; int flag = 0; for (int c = 0 ; c < len/k-1 ; c++) { for (int s = 0 ; s < k ; s++) { if (tmp[c*k+s+i] != tmp[c*k+k+s+i]) flag = 1; } } if (!flag) return len/k; } return -1; } int main () { while (cin >> tmp) { int len = tmp.size(); for (int l = 1 ; l <= len ; l++) { for (int i = 0 ; i <= len-l ; i++) { int j = i+l-1; dp[i][j] = tmp.substr(i,j+1-i); int cnt = isfold(i,j); if (cnt > 0) { char num[10]; sprintf(num,"%d",cnt); string tn = string(num); if (strlen(num)+2+(j-i+1)/cnt < j-i+1) dp[i][j] = tn+"("+dp[i][i+((j-i+1)/cnt)-1]+")"; }else { int index = -1; int length = j-i+1; for (int k = i ; k <= j-1 ; k++) if (length > dp[i][k].size()+dp[k+1][j].size()) { index = k; length = dp[i][k].size()+dp[k+1][j].size(); } if (index != -1) dp[i][j] = dp[i][index]+dp[index+1][j]; } } } cout << dp[0][len-1] << endl; } }
- UVa1630 Folding 记忆化搜索
- [DP记忆化 字符串] UVa1630 Folding
- uva1630(dp记忆化搜索)
- UVA1630 - Folding
- rqnoj-275-FOLDING-记忆化搜索
- UVA1630[Folding] 区间动态规划
- UVa1630 Folding/poj 2176 Folding/zoj 1554 Folding
- UVA 1630 Folding——区间DP(记忆化搜索)
- 【DP 训练】Folding, ACM/ICPC NEERC 2002, UVa1630
- 记忆化搜索例题
- PKU1088---记忆化搜索
- PKU1088 记忆化搜索
- HDU1078 记忆化搜索
- HDU1501 记忆化搜索
- HDU1978 记忆化搜索
- poj1661 记忆化搜索
- zoj1107记忆化搜索
- poj1191(记忆化搜索)
- centos7 二进制安装包安装 mysql5.6
- 微信小程序中设置全局变量
- numpy_operations(Numberical operations on arrays)
- 300.11文件的创建和移动
- Leetcode 152-MaximumProductSubarray 解题报告
- UVa1630 Folding 记忆化搜索
- 何为语法糖?
- 问题:浏览器CSS/JS以怪异模式渲染
- D. Clique Problem
- Vue之for循环
- HTC按键代码
- mysql cpu使用率过高解决方法
- 资源链接
- heap和stack有什么区别