UVa 1630 串折叠(记忆化搜索)

来源:互联网 发布:php json decode 编辑:程序博客网 时间:2024/06/07 15:43

                    UVa 1630 串折叠(记忆化搜索)

UVA - 1630

Folding

Description

Download as PDF

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 Input 

AAAAAAAAAABABABCCDNEERCYESYESYESNEERCYESYESYES

Sample Output 

9(A)3(AB)CCD2(NEERC3(YES))
题意:给出一个由大写字母组成的长度为n(1<=n<=100)的串,“折叠”成一个尽量短的串。

分析:求最短的串,可以明显看出该问题具有最优子结构,动态规划自是不必说,问题的关键在于如何保存和处理那些重复的子串。dp[i][j]代表从字符串的i下标开始一直到j下标结束的压缩后的最短串长度,那么状态转移方程为dp[i][j]=min{dp[i][j],dp[i][k]+dp[k+1][j]},可以用记忆化搜索。先开一个二维string数组,用来保存原串的所有子串,然后在搜索过程中,不断的压缩字符串,同时判断一下重复的次数一起保存到字符串中,递归求解。其他的代码注释给的很详细。

时间:2016/7/21

AC代码:

#include<iostream>#include<cstdio>#include<sstream>#include<cmath>#include<cstring>#include<string>#include<algorithm>#define INF 1<<29#define eps 1e-9#define LD long double#define LL long longconst int maxn = 100 + 10;using namespace std;int dp[maxn][maxn];//str的i下标开始一直到j下标结束的压缩后的最短串长度string sub[maxn][maxn];//保存str所有子串string str;int fold(int l, int r){//枚举法判断重复的长度,不重复返回0for (int i = 1; i <= (r - l + 1) / 2;i++)//枚举长度{int flag = 1;if ((r-l+1)%i) continue;//剪枝for (int j = l; j +i<= r; j++){if (str[j] != str[j + i]){flag = 0;break;}}if (flag) return i;}return 0;}int dfs(int l, int r){int & ans = dp[l][r];//引用if (ans != -1) return ans;if (l == r){sub[l][r] = str[l];return ans = 1;}ans = 1;int res = INF,k;for (int i = l; i < r; ++i){int temp1 = dfs(l, i) + dfs(i + 1, r);//状态转移if (temp1 < res) {k = i;res = temp1; }}sub[l][r] = sub[l][k] + sub[k + 1][r];int len= fold(l, r);if (len){int rep = (r - l + 1) / len;stringstream ss;//类似toString()方法ss<< rep;string t=ss.str();string newstr = t + string("(") + sub[l][l + len-1] + string(")");int temp2 = newstr.size();if (temp2 < res){res = temp2;sub[l][r] = newstr;}}return ans = res;}int main(){#ifdef LOCALfreopen("data.txt", "r", stdin);#endifwhile (cin >> str){memset(dp, -1, sizeof(dp));int right = str.size() - 1;dfs(0, right);cout << sub[0][right] << endl;}return 0;}




1 0
原创粉丝点击