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

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

UVA - 1630

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 

AAAAAAAAAABABABCCD
NEERCYESYESYESNEERCYESYESYES

Sample Output 

9(A)3(AB)CCD
2(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 long
const 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)
{
	//枚举法判断重复的长度,不重复返回0
	for (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 LOCAL
	freopen("data.txt", "r", stdin);
#endif
	while (cin >> str)
	{
		memset(dp, -1, sizeof(dp));
		int right = str.size() - 1;
		dfs(0, right);
		cout << sub[0][right] << endl;
	}
	return 0;
}





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值