UVA1630 - Folding

Bill is trying to compactly represent sequences of capital alphabetic characters from A' toZ’ by folding repeating subsequences inside them. For example, one way to represent a sequence AAAAAAAAAABABABCCD' is10(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' toZ’ 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' toZ’ 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))

我有话说:
这道题是一道字符串类型的动态规划。所以按照一般思路,我们考虑对一个字符串分成两段处理。即dp(i,j)的最小长度为dp(i,k)的最小长度加上dp(k+1,j)的最小长度。其中i<=k<=j。同时把每小段字符串压缩后的字符串记录在flod数组里面。
转移方程为dp(i,j)=min{dp(i,k)+dp(k+1,j);
细节参考注释。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
using namespace std;

const int maxn=100+10;
const int INF=1000000;
int dp[maxn][maxn];
string flod[maxn][maxn];
string str;
int judge(int L,int R)
{
    for(int i=1;i<=(R-L+1)/2;i++){
        if((R-L+2)%i)continue;//不能分为整数段
        bool flag=true;
        for(int j=L;j+i<=R;j++){
            if(str[j]!=str[j+i]){
                flag=false;
                break;
            }
        }
        if(flag)return i;
    }
    return 0;
}
int solve(int L,int R)
{
    int& ans=dp[L][R];
    if(ans!=-1)return ans;
    if(L==R){
        flod[L][R]=str[L];
        return ans=1;
    }
    ans=INF;
    int k,t;
    for(int i=L;i<=R;i++){
        t=solve(L,i)+(i+1,R);
        if(t<ans){
            ans=t;
            k=i;
        }
    }
    flod[L][R]=flod[L][k]+flod[k+1][R];
    int len=judge(L,R);
    if(len){
        bool test=true;
        for(int i=L;i<=R;i++){
            if(str[i]=='('||str[i]==')')test=false;//不能把括号作为压缩对象
        }
        char t[10];
        sprintf(t,"%d",(R-L+1)/len);
        string newstr=t+string("(")+flod[L][L+len-1]+string(")");
        if(test&&newstr.size()<ans){
            ans=newstr.size();
            flod[L][R]=newstr;
        }
    }
    return ans;
}
int main()
{
    while(cin>>str){
        memset(dp,-1,sizeof(dp));
        int len=str.size()-1;
        solve(0,len);
        cout<<flod[0][len]<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值